> ## Documentation Index
> Fetch the complete documentation index at: https://docs.scriptonia.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Platform API

>  POST /api/platform — Get platform options and a recommended platform from refined requirements.

## Endpoint

```
POST /api/platform
Content-Type: application/json
```

***

## Request Format

| Field                 | Type    | Required | Description                                                        |
| :-------------------- | :------ | :------- | :----------------------------------------------------------------- |
| `sessionId`           | string  | Yes      | From `/api/intent` response.                                       |
| `refinedRequirements` | object  | No       | Fallback if context has no `refinedRequirements`.                  |
| `supabaseIntegration` | boolean | yes      | When true, Supabase is included in the blueprint where applicable. |

### Example

```
{
  "sessionId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "supabaseIntegration": true
}
```

***

## Response Format

### Success (200)

| Field              | Type                   | Description                                                                                                                       |
| :----------------- | :--------------------- | :-------------------------------------------------------------------------------------------------------------------------------- |
| `status`           | `"success"`            |                                                                                                                                   |
| `stage`            | `"platform_selection"` |                                                                                                                                   |
| `platform_options` | array                  | Each: `platform`, `description`, `cost_script`, `timeline_seconds`, `agents_required[]`, `fit_score` (and optional `tech_stack`). |
| `recommended`      | string                 | One of: `Website`, `Web Application`, `Android`, `iOS`.                                                                           |
| `next_stage`       | `"payment_processing"` |                                                                                                                                   |

### Example

```
{
  "status": "success",
  "stage": "platform_selection",
  "platform_options": [
    {
      "platform": "Web Application",
      "description": "Fits SaaS with auth and real-time; full backend and DB.",
      "cost_script": 10000,
      "timeline_seconds": 75,
      "agents_required": ["ProductManager", "FrontendDev", "BackendDev", "DevOpsEngineer", "ToolManager"],
      "fit_score": 95
    }
  ],
  "recommended": "Web Application",
  "next_stage": "payment_processing"
}
```

**Note:** `cost_script` is always `UNIFIED_PRICE_SCRIPT` (e.g. 10000); the handler overwrites values from `selectPlatform`.

***

### Error (4xx / 5xx)

| Field    | Description            |
| :------- | :--------------------- |
| `status` | `"error"`              |
| `stage`  | `"platform_selection"` |
| `error`  | Message.               |

***

## Error Codes

| HTTP | Cause                                                                                                                                |
| :--- | :----------------------------------------------------------------------------------------------------------------------------------- |
| 400  | `sessionId` missing; workflow not found and no `refinedRequirements` to rebuild; `refinedRequirements` still missing after recovery. |
| 405  | Method not POST.                                                                                                                     |
| 500  | `selectPlatform` (OpenRouter) failure; DB or schema issues (e.g. `supabaseIntegration` when migration not run).                      |

***

## Implementation Notes

* Loads context via `getWorkflowContextDB` then `getWorkflowContext`. If `refinedRequirements` is missing, uses `refinedRequirements` from the body when provided.
* Calls `selectPlatform(refinedRequirements, sessionId)` from `lib/openai.ts` (PlatformSelector). Normalizes each option’s `cost_script` to `UNIFIED_PRICE_SCRIPT`.
* Persists: `platformOptions` (normalized), `supabaseIntegration`, `currentStage: 'platform_selection'`. `platformOptions` may not be stored in DB; in-memory is used as fallback.

***
