# Answer a sandbox Connect Session's consent screen as its end user would, without a browser

`POST https://api.koil.co/v1/sandbox/connect-sessions/{connectSessionId}/complete`

Answer a sandbox Connect Session's consent screen as its end user would, without a browser: the session ends `completed` or `failed` as the browser flow ends it, with the same Connection and `connection.*` events. A session whose `url` was already opened is refused with `conflict`. Sandbox keys only.

## Authorization

An API key from the Koil admin console, sent as `Authorization: Bearer <key>`. A live key starts `koil_live_` and a sandbox key `koil_sandbox_`; the key alone selects the Organization and mode. See https://docs.koil.co/concepts/authentication.

## Parameters

| Name | In | Required | Type | Description |
| --- | --- | --- | --- | --- |
| `Koil-Version` | header | yes | `"2026-12-08"` | The dated API version the request is written against; it fixes the request and response shapes. Every response echoes the version it is rendered in. |
| `connectSessionId` | path | yes | string | Koil Connect Session identifier. Not a secret: only `url` can start the flow. |

## Request body

`application/json`

- `outcome` (one of `"approved"`, `"partial_scopes"`, `"denied"`, required): What the end user does on the consent screen. `approved` grants every scope the issuer's platforms need, as opening the session's `url` does. `partial_scopes` grants only what listing their platform profiles needs, as a user who unticks permissions does. `denied` refuses, as a user who cancels does.

Every named shape is in the [OpenAPI document](https://docs.koil.co/openapi.json) under `components.schemas`.

## Example

Have the end user approve:

```bash
curl -X POST "https://api.koil.co/v1/sandbox/connect-sessions/{connectSessionId}/complete" \
  -H "Authorization: Bearer $KOIL_API_KEY" \
  -H "Koil-Version: 2026-12-08" \
  -H "Content-Type: application/json" \
  -d '{"outcome":"approved"}'
```

## Response 200

The Connect Session, now `completed` or `failed`.

- `kind` (`"connect_session"`, required): The kind of resource this is.
- `mode` (one of `"live"`, `"sandbox"`, required): Which mode the resource belongs to. Live and sandbox resources are separate: a key reads and writes only its own mode's.
- `id` (string, required) (min length 1): Koil Connect Session identifier. Not a secret: only `url` can start the flow.
- `appId` (string, required) (min length 1): Koil App identifier: your OAuth app that authorizes the Connection.
- `clientUserId` (string) (max length 255): Your own identifier for the end user the Connection belongs to.
- `connectionId` (string) (min length 1): The Connection the session re-authorizes, or, once `completed`, the one it created.
- `returnUrl` (string, uri, required) (max length 2048): Where Koil sends the end user's browser once the session ends, or when its link is opened again, with `connectSessionId` and the session's `status` added to its query (merged into any query it already has): `completed`, `failed`, `expired`, or `pending` when the link was already opened, perhaps in another browser where the end user may still be finishing. Unless it is `completed`, create a new Connect Session and redirect them to its `url` if they need to start over. Must be `https:`.
- `status` (one of `"pending"`, `"completed"`, `"failed"`, `"expired"`, required): `pending` until the end user finishes; `completed` once the Connection is authorized (`connectionId` names it); `failed` when the platform or the end user refused (`error` says why); `expired` once `expiresAt` passes with the session still pending. Every state but `pending` is final.
- `error` (object)
  - `code` (one of `"validation_error"`, `"not_supported"`, `"unauthorized"`, `"quota_exceeded"`, `"permission_denied"`, `"scope_insufficient"`, `"connection_invalid"`, `"not_found"`, `"conflict"`, `"rate_limit_exceeded"`, `"platform_rate_limit"`, `"internal"`, `"service_unavailable"`, required): Stable error code. `validation_error` (400), `not_supported` (400), `unauthorized` (401), `quota_exceeded` (402), `permission_denied` (403), `scope_insufficient` (403), `connection_invalid` (403), `not_found` (404), `conflict` (409), `rate_limit_exceeded` (429), `platform_rate_limit` (429), `internal` (500, 502, 504), `service_unavailable` (503).
  - `message` (string, required): Human-readable summary.
  - `meta` (object, required): Structured detail; the keys depend on `code`.
    - `violations` (array of object): Every problem with the request. Always present on a `validation_error`.
      - `code` (string, required): Stable violation code, such as `missing_field`, `unknown_field`, `schema_error`, `invalid_value` or `invalid_combination`.
      - `path` (string, required): JSON pointer into the request: path, query and body fields share one pointer space (`/cursor`, `/publishRequests/0/args/text`), a header is `/headers/<lowercase name>`, and `""` is the request as a whole.
      - `message` (string, required): Human-readable explanation.
    - `budgets` (array of string): On a `rate_limit_exceeded`: every budget that refused the request: `organization` (every authenticated request), `profile` (a request that acts on a Profile, one budget per Profile) or `client_address` (requests with no key or a rejected one). Koil may add budgets; treat an unknown one as covering the whole Organization.
    - `profileIds` (array of string): On a `rate_limit_exceeded` whose `budgets` include `profile`: the Profiles whose budget is spent. Requests that act on other Profiles are unaffected.
    - `required` (array of string): On a `permission_denied`: the API key permissions the operation requires that the key lacks.
    - `limit` (integer): On a `quota_exceeded`: the cap that was reached.
    - `resourceType` (string): On a `not_found` or a `conflict` over an existing resource: the kind of resource, such as `profile` or `connection`.
    - `resourceId` (string): With `resourceType`: the id of the resource not found or already there.
    - `reason` (string): On a failed publish or backfill request, or a failed Connect Session, that Koil failed itself, not the platform: why, such as `media_incompatible`, `media_transform_not_entitled` or `subject_mismatch` (a reconnect authorized a different platform account than the Connection's).
    - `refusalReason` (one of `"messaging_window_closed"`, `"recipient_rate_limited"`, `"recipient_unavailable"`, `"publishing_limit_reached"`): Why the platform refused, where the code alone does not say what to do. `messaging_window_closed`: The platform takes a message to this person only inside a window that has closed: WhatsApp's 24-hour customer service window, the Messenger and Instagram messaging window, or TikTok's allowance since the person last wrote. It reopens when the person writes again. `recipient_rate_limited`: Too many messages to this one person in a short time; messages to others are unaffected. Retry after `Retry-After`. `recipient_unavailable`: The person cannot receive messages from this Profile: they blocked it or deactivated their account, or, on WhatsApp, the number has no account or an outdated app. Retrying does not help. `publishing_limit_reached`: The account's own publishing cap on the platform is spent: Instagram's 24-hour content publishing limit, YouTube's daily upload limit, or TikTok's limit on posts. Publish again once it resets.
- `grantedScopes` (array of string): Once `completed`, the scopes the end user granted, as the platform reported them, sorted. Absent when the platform reported none, which is not a grant of none.
- `missingScopes` (array of string): Once `completed`, the scopes the session requested (the App's `scopes` when the end user opened the link) that the end user did not grant, sorted. Facebook Login and Google let the end user decline any of them, and the session completes all the same. Absent when every scope was granted or the platform reported none. An operation that needs a missing scope is refused `scope_insufficient`: if your product needs one, send the end user through a new Connect Session with this `connectionId`. No event carries it: read the session once `connection.created` (or `connection.updated`) arrives.
- `expiresAt` (string, date-time, required): When the session can no longer be started or finished.
- `metadata` (object, required): Your own key-value pairs, up to 50 keys of at most 40 characters with string values of at most 500. An update merges into what is stored, and a key set to `""` is removed.
- `createdAt` (string, date-time, required)
- `updatedAt` (string, date-time, required)

## Errors

Every error has the same shape: `{ "error": { "code", "message", "meta", "requestId" } }`. [Errors →](https://docs.koil.co/concepts/errors)

| Status | Meaning |
| --- | --- |
| 400 | `validation_error`: The request does not match the operation's input schema, fails an admission check, or the platform refused its content. `meta.violations` names every problem. `not_supported`: The platform, collection or resource does not offer this operation, such as exporting a `key` Connection's credential, which Koil never returns. |
| 401 | `unauthorized`: No API key, or a key Koil does not accept (unknown, revoked, or with a prefix that does not match its mode). |
| 404 | `not_found`: The resource does not exist in this Organization and mode. |
| 409 | `conflict`: The request collides with current state: an idempotency key reused with a different payload or still in flight, a run already in progress, a publish that has already started, or a resource that already exists (`meta.resourceType` and `meta.resourceId` name it). Retry only when the response carries `Retry-After` (the key's first request is still in flight, or a sandbox Profile's collection hasn't had its first poll), after that many seconds. |
| 429 | `rate_limit_exceeded`: Koil's own rate limit for the key, Organization, Profile, or client address is spent (`meta.budgets` names which); retry after `Retry-After` seconds. `platform_rate_limit`: The platform throttled the call or the platform quota is spent; retry after `Retry-After` seconds. |
| 500 | `internal`: Koil or the platform failed, and the status says which: 500 when Koil failed, including a platform refusing how Koil made the call; 502 when the platform failed or answered something Koil cannot use; 504 when Koil's call to the platform timed out. The message is generic; retry with backoff. |
| 503 | `service_unavailable`: A service Koil needs to accept the request (API key validation or rate limiting) is unavailable. Nothing was attempted; retry after `Retry-After` seconds. The hosting edge can deliver this as a `504` with neither this body nor `Retry-After`, so treat a `502`, `503` or `504` alike as retryable, with backoff. |
| default | Any other failure. `error.code` names it; the HTTP status follows from it. |
