# Link the org to SSO

> For AI agents: the index of these docs is at https://referalli.com/docs/llms.txt (every page it lists has a Markdown version like this one).

## Link the org to SSO

**`POST https://referalli.com/v1/org/sso/link`** — Link the org to SSO

Scope: `org:write`

Creates the org's directory tenant at the SSO provider, with the calling owner (who must have signed in with the provider) as its first admin. Owner session only. 409 when already linked elsewhere. Live mode only.

**Auth:** an API key with `org:write`, or a session with role `owner`.

**Scopes:** `org:write`, `full`, `admin`.

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `Idempotency-Key` | header | string | no | Any unique string up to 255 printable ASCII characters (a UUID). A retry with the same key within 24 hours gets the first response again (with `idempotent-replayed: true`) and nothing runs twice. The same key on a different request is `422 idempotency_key_reused`; while the first is still running, `409 idempotency_in_progress`. |
| `x-org-id` | header | string | no | With a session: the org to act in (the user must be a member). Not used with API keys, which belong to one org. |
| `referalli-mode` | header | string | no | `test` runs a session request against the org's test data. With an API key it must match the key's mode (`400 mode_mismatch` otherwise). The header name from before the rename is still accepted. |

### Responses

**200** — OK

#### Fields

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `linked` | boolean | yes |  |
| `provider` | string | yes |  |
| `tenantId` | string | yes |  |
| `atlasOrgId` | string | yes |  |

```json
{
  "linked": true,
  "provider": "string",
  "tenantId": "string",
  "atlasOrgId": "string"
}
```

**400** — The request is invalid. `error.code` is one of:

- `validation_error`: The body or query failed validation, an unknown field was sent, the body is not valid JSON, or a session call has no `x-org-id`. `details` lists the failing fields.
- `mode_mismatch`: The `Referalli-Mode` header disagrees with the API key's mode.
- `bad_request`: Rejected by the HTTP layer before the handler.
- `invalid_handoff_token`: `handoffToken` on `associate` is malformed, expired (24 hours), signed for another org, or its signature does not verify.

#### Fields

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `error` | object | yes |  |
| `error.code` | string | yes |  |
| `error.message` | string | yes |  |
| `error.details` |  | no |  |
| `error.requestId` | string | no |  |
| `error.docs_url` | string | yes | The docs entry for this error code: what it means and how to fix it. |

**validation_error**

```json
{
  "error": {
    "code": "validation_error",
    "message": "the body or query failed validation, an unknown field was sent, the body is not valid json, or a session call has no `x-org-id`",
    "details": [
      {
        "code": "invalid_format",
        "path": [
          "destinationUrl"
        ],
        "message": "Invalid URL"
      }
    ],
    "docs_url": "https://referalli.com/docs/errors-and-limits#validation_error"
  }
}
```

**mode_mismatch**

```json
{
  "error": {
    "code": "mode_mismatch",
    "message": "the `referalli-mode` header disagrees with the api key's mode",
    "docs_url": "https://referalli.com/docs/errors-and-limits#mode_mismatch"
  }
}
```

**bad_request**

```json
{
  "error": {
    "code": "bad_request",
    "message": "rejected by the http layer before the handler",
    "docs_url": "https://referalli.com/docs/errors-and-limits#bad_request"
  }
}
```

**invalid_handoff_token**

```json
{
  "error": {
    "code": "invalid_handoff_token",
    "message": "`handofftoken` on `associate` is malformed, expired (24 hours), signed for another org, or its signature does not verify",
    "docs_url": "https://referalli.com/docs/errors-and-limits#invalid_handoff_token"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**401** — Missing or invalid credentials. `error.code` is one of:

- `unauthorized`: No credentials, an unknown, revoked or expired API key or session, or a signature that did not verify.
- `step_up_required`: A partner with two-factor on must send `x-totp-code`.
- `reauth_required`: A session that signed in too long ago changed payout details; `details` is `{ "reauthUrl", "maxAgeMinutes" }`.

Fields: as for `400` of `POST /v1/org/sso/link`.

**unauthorized**

```json
{
  "error": {
    "code": "unauthorized",
    "message": "no credentials, an unknown, revoked or expired api key or session, or a signature that did not verify",
    "docs_url": "https://referalli.com/docs/errors-and-limits#unauthorized"
  }
}
```

**step_up_required**

```json
{
  "error": {
    "code": "step_up_required",
    "message": "a partner with two-factor on must send `x-totp-code`",
    "docs_url": "https://referalli.com/docs/errors-and-limits#step_up_required"
  }
}
```

**reauth_required**

```json
{
  "error": {
    "code": "reauth_required",
    "message": "a session that signed in too long ago changed payout details",
    "docs_url": "https://referalli.com/docs/errors-and-limits#reauth_required"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**403** — Authenticated, but not allowed. `error.code` is one of:

- `forbidden`: The wrong role, a key without the needed scope (the message names it; `details.requiredScopes` lists every scope that would do), not a member, a suspended org, or a publishable key from an origin it does not allow.
- `sso_required`: This organization requires single sign-on; `details.signInUrl` starts it.
- `access_suspended`: Your identity provider suspended your access and you are the last owner.
- `unsupported_media_type`: The request body's `Content-Type` isn't one this endpoint reads. `details.received` is the header as sent, `details.accepted` what to send instead (`application/json`). Status 415.
- `program_scoped_key`: A key limited to some programs (`programIds`) called an org-wide endpoint; `details.alternative` names the key that can. A connection create for a program outside the key also answers this, with `details.programId`.
- `restricted_session`: A partner session opened from a merchant portal link is read-only for one program; sign in normally to make changes.

Fields: as for `400` of `POST /v1/org/sso/link`.

**forbidden**

```json
{
  "error": {
    "code": "forbidden",
    "message": "the wrong role, a key without the needed scope (the message names it",
    "docs_url": "https://referalli.com/docs/errors-and-limits#forbidden"
  }
}
```

**sso_required**

```json
{
  "error": {
    "code": "sso_required",
    "message": "this organization requires single sign-on",
    "docs_url": "https://referalli.com/docs/errors-and-limits#sso_required"
  }
}
```

**access_suspended**

```json
{
  "error": {
    "code": "access_suspended",
    "message": "your identity provider suspended your access and you are the last owner",
    "docs_url": "https://referalli.com/docs/errors-and-limits#access_suspended"
  }
}
```

**unsupported_media_type**

```json
{
  "error": {
    "code": "unsupported_media_type",
    "message": "the request body's `content-type` isn't one this endpoint reads",
    "docs_url": "https://referalli.com/docs/errors-and-limits#unsupported_media_type"
  }
}
```

**program_scoped_key**

```json
{
  "error": {
    "code": "program_scoped_key",
    "message": "a key limited to some programs (`programids`) called an org-wide endpoint",
    "docs_url": "https://referalli.com/docs/errors-and-limits#program_scoped_key"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**404** — No such resource. `error.code` is one of:

- `not_found`: The resource does not exist in this org (ids from other orgs look the same), a referenced id does not exist, a list cursor is not in the list, or the endpoint does not exist.
- `not_enrolled`: The user is not a partner in the program (and the widget's autoEnroll is off, or no verified email was given).

Fields: as for `400` of `POST /v1/org/sso/link`.

**not_found**

```json
{
  "error": {
    "code": "not_found",
    "message": "the resource does not exist in this org (ids from other orgs look the same), a referenced id does not exist, a list cursor is not in the list, or the endpoint does not exist",
    "docs_url": "https://referalli.com/docs/errors-and-limits#not_found"
  }
}
```

**not_enrolled**

```json
{
  "error": {
    "code": "not_enrolled",
    "message": "the user is not a partner in the program (and the widget's autoenroll is off, or no verified email was given)",
    "docs_url": "https://referalli.com/docs/errors-and-limits#not_enrolled"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**409** — The request conflicts with the current state. `error.code` is one of:

- `conflict`: A unique value is taken, or the action would break a rule (the message says which).
- `billing_connection_no_key`: The Stripe billing connection keeps no key to reuse for payouts (only one connected with `stripeKey` and `creditRail` does): send `secretKey` instead.
- `api_key_expired`: This key has expired, so it can no longer be rotated; mint a new one.
- `idempotency_in_progress`: A request with the same `Idempotency-Key` is still running; retry with the same key.
- `stripe_key_required`: Stripe balance credits need a Stripe key: `PUT /v1/stripe-credits/connection` or a Stripe payout connection.
- `credit_credentials_required`: The account-credit rail's billing connection has no API credentials: `PUT /v1/billing/connections/{id}/credentials`.
- `replay`: A signed auth-connection call reused an id already received.
- `live_mode_only`: Only changeable in live mode.
- `test_mode_only`: Only exists in test mode.
- `test_mode_unsupported`: Not available in test mode.
- `partners_owed`: An erasure would remove partners still owed commissions (pending or approved); `details.partnerIds` names them. Pay out or settle first.
- `no_payout_connection`: A payout run for programs no active payout connection funds (no program-bound one lists them, no org-wide one); `details.programIds` names them.

Fields: as for `400` of `POST /v1/org/sso/link`.

**conflict**

```json
{
  "error": {
    "code": "conflict",
    "message": "a unique value is taken, or the action would break a rule (the message says which)",
    "docs_url": "https://referalli.com/docs/errors-and-limits#conflict"
  }
}
```

**billing_connection_no_key**

```json
{
  "error": {
    "code": "billing_connection_no_key",
    "message": "the stripe billing connection keeps no key to reuse for payouts (only one connected with `stripekey` and `creditrail` does)",
    "docs_url": "https://referalli.com/docs/errors-and-limits#billing_connection_no_key"
  }
}
```

**api_key_expired**

```json
{
  "error": {
    "code": "api_key_expired",
    "message": "this key has expired, so it can no longer be rotated",
    "docs_url": "https://referalli.com/docs/errors-and-limits#api_key_expired"
  }
}
```

**idempotency_in_progress**

```json
{
  "error": {
    "code": "idempotency_in_progress",
    "message": "a request with the same `idempotency-key` is still running",
    "docs_url": "https://referalli.com/docs/errors-and-limits#idempotency_in_progress"
  }
}
```

**stripe_key_required**

```json
{
  "error": {
    "code": "stripe_key_required",
    "message": "stripe balance credits need a stripe key",
    "docs_url": "https://referalli.com/docs/errors-and-limits#stripe_key_required"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**422** — The request cannot be processed as sent. `error.code` is one of:

- `idempotency_key_reused`: The `Idempotency-Key` was used for a different request.
- `validation_error`: On the signed auth-connection endpoints: an unknown or invalid field, after the signature checked out.
- `stripe_key_invalid`: Stripe rejected the Stripe balance credit key (revoked, rolled or mistyped).
- `stripe_key_wrong_mode`: A Stripe balance credit key of the other mode: live mode takes `rk_live_`/`sk_live_`, test mode `rk_test_`/`sk_test_`.
- `stripe_key_permission`: The restricted Stripe key lacks a permission the call needs (Webhook Endpoints write; Customers write with `creditRail`; Transfers write for payouts).
- `provider_credentials_invalid`: The billing provider rejected the credentials (`autoRegister`, or the account-credit rail's connection credentials); nothing was saved.
- `provider_credentials_wrong_mode`: Account-credit rail credentials of the other mode (a Chargebee `…-test` site in live mode, or another site in test mode).
- `provider_permission`: The credentials are valid but cannot manage webhooks (`autoRegister`) or write customer credits (account-credit rail).
- `unknown_topic`: A webhook endpoint topic that is not published (nor `*` or a `prefix.*` covering one); the message suggests the closest and `details.validTopics` lists them.

Fields: as for `400` of `POST /v1/org/sso/link`.

**idempotency_key_reused**

```json
{
  "error": {
    "code": "idempotency_key_reused",
    "message": "the `idempotency-key` was used for a different request",
    "docs_url": "https://referalli.com/docs/errors-and-limits#idempotency_key_reused"
  }
}
```

**validation_error**

```json
{
  "error": {
    "code": "validation_error",
    "message": "on the signed auth-connection endpoints",
    "docs_url": "https://referalli.com/docs/errors-and-limits#validation_error"
  }
}
```

**stripe_key_invalid**

```json
{
  "error": {
    "code": "stripe_key_invalid",
    "message": "stripe rejected the stripe balance credit key (revoked, rolled or mistyped)",
    "docs_url": "https://referalli.com/docs/errors-and-limits#stripe_key_invalid"
  }
}
```

**stripe_key_wrong_mode**

```json
{
  "error": {
    "code": "stripe_key_wrong_mode",
    "message": "a stripe balance credit key of the other mode",
    "docs_url": "https://referalli.com/docs/errors-and-limits#stripe_key_wrong_mode"
  }
}
```

**stripe_key_permission**

```json
{
  "error": {
    "code": "stripe_key_permission",
    "message": "the restricted stripe key lacks a permission the call needs (webhook endpoints write",
    "docs_url": "https://referalli.com/docs/errors-and-limits#stripe_key_permission"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**429** — Rate limited. `error.code` is one of:

- `rate_limited`: An endpoint limit or the per-key quota was exceeded; wait `Retry-After` seconds.

Fields: as for `400` of `POST /v1/org/sso/link`.

**rate_limited**

```json
{
  "error": {
    "code": "rate_limited",
    "message": "an endpoint limit or the per-key quota was exceeded",
    "docs_url": "https://referalli.com/docs/errors-and-limits#rate_limited"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |
| `Retry-After` | integer | no | Seconds until a retry can succeed. |
| `RateLimit-Limit` | integer | no | The quota bucket's size (the burst). |
| `RateLimit-Remaining` | integer | no | Requests that can be made right now. |
| `RateLimit-Reset` | integer | no | Seconds until the bucket is full again. |

**500** — Unexpected server error. `error.code` is one of:

- `internal_error`: Retry with backoff; quote `requestId` if it persists.

Fields: as for `400` of `POST /v1/org/sso/link`.

**internal_error**

```json
{
  "error": {
    "code": "internal_error",
    "message": "retry with backoff",
    "requestId": "req_8f2c1d4e5a6b7c8d",
    "docs_url": "https://referalli.com/docs/errors-and-limits#internal_error"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**502** — An upstream provider failed. `error.code` is one of:

- `sso_provider_error`: The SSO provider returned an error; `details.provider` and `details.providerCode` say which.
- `atlas_error`: Deprecated name of `sso_provider_error`; no longer returned.
- `stripe_unavailable`: Stripe could not be reached to check a Stripe balance credit key; retry.
- `provider_unavailable`: The billing provider could not be reached or refused the request; nothing was saved. Retry.

Fields: as for `400` of `POST /v1/org/sso/link`.

**sso_provider_error**

```json
{
  "error": {
    "code": "sso_provider_error",
    "message": "the sso provider returned an error",
    "docs_url": "https://referalli.com/docs/errors-and-limits#sso_provider_error"
  }
}
```

**atlas_error**

```json
{
  "error": {
    "code": "atlas_error",
    "message": "deprecated name of `sso_provider_error`",
    "docs_url": "https://referalli.com/docs/errors-and-limits#atlas_error"
  }
}
```

**stripe_unavailable**

```json
{
  "error": {
    "code": "stripe_unavailable",
    "message": "stripe could not be reached to check a stripe balance credit key",
    "docs_url": "https://referalli.com/docs/errors-and-limits#stripe_unavailable"
  }
}
```

**provider_unavailable**

```json
{
  "error": {
    "code": "provider_unavailable",
    "message": "the billing provider could not be reached or refused the request",
    "docs_url": "https://referalli.com/docs/errors-and-limits#provider_unavailable"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |

**503** — Not available on this deployment right now. `error.code` is one of:

- `webhooks_disabled`: This inbound webhook is not configured on the deployment.
- `sso_not_configured`: Single sign-on is not set up on this deployment.

Fields: as for `400` of `POST /v1/org/sso/link`.

**webhooks_disabled**

```json
{
  "error": {
    "code": "webhooks_disabled",
    "message": "this inbound webhook is not configured on the deployment",
    "docs_url": "https://referalli.com/docs/errors-and-limits#webhooks_disabled"
  }
}
```

**sso_not_configured**

```json
{
  "error": {
    "code": "sso_not_configured",
    "message": "single sign-on is not set up on this deployment",
    "docs_url": "https://referalli.com/docs/errors-and-limits#sso_not_configured"
  }
}
```

#### Headers

| Header | Type | Required | Description |
| --- | --- | --- | --- |
| `x-request-id` | string | no | This request's id (`req_…`), matching the server logs. On every response. |
| `Retry-After` | integer | no | Seconds until a retry can succeed. |

**default** — Any other error, in the same envelope.

Fields: as for `400` of `POST /v1/org/sso/link`.

```json
{
  "error": {
    "code": "string",
    "message": "string",
    "details": {},
    "requestId": "string",
    "docs_url": "string"
  }
}
```

### Code samples

**cURL**

```curl
curl -X POST "https://referalli.com/v1/org/sso/link" \
  -H "Authorization: Bearer $REFERALLI_API_KEY" \
  -H "Idempotency-Key: <value>" \
  -H "x-org-id: <value>" \
  -H "referalli-mode: <value>"
```

**TypeScript**

```typescript
const res = await fetch('https://referalli.com/v1/org/sso/link', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer $REFERALLI_API_KEY',
    'Idempotency-Key': '<value>',
    'x-org-id': '<value>',
    'referalli-mode': '<value>',
  },
});
const data = await res.json();
```

**Python**

```python
import requests

res = requests.request(
    "POST",
    "https://referalli.com/v1/org/sso/link",
    headers={
    "Authorization": "Bearer $REFERALLI_API_KEY",
    "Idempotency-Key": "<value>",
    "x-org-id": "<value>",
    "referalli-mode": "<value>",
    },
)
data = res.json()
```

**Go**

```go
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, _ := http.NewRequest("POST", "https://referalli.com/v1/org/sso/link", nil)
	req.Header.Set("Authorization", "Bearer $REFERALLI_API_KEY")
	req.Header.Set("Idempotency-Key", "<value>")
	req.Header.Set("x-org-id", "<value>")
	req.Header.Set("referalli-mode", "<value>")
	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)
	fmt.Println(string(body))
}
```

**PHP**

```php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://referalli.com/v1/org/sso/link');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer $REFERALLI_API_KEY', 'Idempotency-Key: <value>', 'x-org-id: <value>', 'referalli-mode: <value>']);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
```

**Ruby**

```ruby
require 'net/http'
require 'uri'

uri = URI('https://referalli.com/v1/org/sso/link')
req = Net::HTTP::Post.new(uri)
req['Authorization'] = 'Bearer $REFERALLI_API_KEY'
req['Idempotency-Key'] = '<value>'
req['x-org-id'] = '<value>'
req['referalli-mode'] = '<value>'
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
  http.request(req)
end
puts res.body
```

**Java**

```java
import java.net.URI;
import java.net.http.*;
import java.net.http.HttpRequest.BodyPublishers;

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://referalli.com/v1/org/sso/link"))
    .header("Authorization", "Bearer $REFERALLI_API_KEY")
    .header("Idempotency-Key", "<value>")
    .header("x-org-id", "<value>")
    .header("referalli-mode", "<value>")
    .method("POST", BodyPublishers.noBody())
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

---

## For AI agents

- This page for a human reader: https://referalli.com/docs/ref-postv1orgssolink
- The whole space as one Markdown file: https://referalli.com/docs/llms-full.txt
- Live search + fetch over MCP: https://referalli.com/docs/_api/public/orgs/07f432f4-34b9-46a7-aaad-c0e395d2e4f9/spaces/2bf4f614-3630-4fbf-85f9-5ecafa6b7d97/mcp
  - Install (Claude CLI): `claude mcp add referalli --transport http https://referalli.com/docs/_api/public/orgs/07f432f4-34b9-46a7-aaad-c0e395d2e4f9/spaces/2bf4f614-3630-4fbf-85f9-5ecafa6b7d97/mcp`

This Markdown is the same content the page renders — safe to quote and to ground answers in.
