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

# Multi-user auth over the API

> Run one agent for many end users over the API: who the run acts as, how each user's MCP connectors get that user's credentials (one token per request, a sign-in link in the stream, or a token per MCP connector), and which API key to use.

One backend can call one agent for many end users. Each call says who the user is, and each MCP connector the agent uses gets that user's own credential. This page covers the three ways to give the credential and the API key rules that decide who the run acts as.

## Who the run acts as

The API key decides whether the request body can name the user.

| API key | `input.user` in the body | The run acts as |
| - | - | - |
| The organization default key | Used as sent | The user in `input.user` |
| Any other organization key, a personal key, or a key limited to some agents | Left out, or names the key's owner. Any other user is refused with 403 `run_user_not_allowed` | The person the key was issued to |
| The organization default key, no `input.user` | Not sent | The agent's default user, when the agent has one |

> [!WARNING]
>
> Only the organization default key can name any user. Any other key runs as its owner: when `input.user` names someone else, the call fails with 403 `run_user_not_allowed`. A key limited to some agents is never the default key, so it always runs as its owner. To run one agent for many users, call it with the organization default key.

An agent with **API callers name the end user** turned on is the exception. On that agent, a key other than the default key may name an end user of your app in `input.user`. See [Run as your end users](https://docs.xpander.ai/api-reference/end-users.md).

Send `input.user.id` as a stable id from your system. Per-user sign-ins are stored under this id, so the same id must come back on the next call for the same person. `input.user.email` is required when you send `input.user`.

## Give each user's credential to the MCP connectors

Pick one method per agent.

| Method | You send | Fits when |
| - | - | - |
| [One token per request](#one-token-per-request) | One identity provider token for the user, in `Authorization: Bearer` or `user_oidc_token` | Your users sign in to your app with an OIDC identity provider that supports token exchange (RFC 8693) |
| [Sign-in link in the stream](#sign-in-link-in-the-stream) | Only `input.user` | The MCP connector uses OAuth and the user can open a link once |
| [A token per MCP connector](#a-token-per-mcp-connector) | `user_tokens`, one entry per MCP connector | You already hold a separate token for each MCP connector |

### One token per request

Send one token with the request. xpander exchanges it at your identity provider (OIDC token exchange, RFC 8693) for a token with the MCP audience, and every MCP connector on the agent that uses the pre-auth token (on by default) gets that exchanged token as `Authorization: Bearer`. MCP connectors set to **SSO user** sign-in keep their own setting.

Set it up once:

1. In **Platform > Organization > Access > Single sign-on**, fill the **Service app** card and turn on **Agent pre-authentication** with your audiences and token exchange scopes. See [Agent pre-authentication](https://docs.xpander.ai/self-hosted/sso.md#agent-pre-authentication).
2. Turn it on for the agent with [Update Agent](https://docs.xpander.ai/api-reference/v1/agents/update-agent.md):

```bash theme={"dark"}
curl -X PATCH "https://api.xpander.ai/v1/agents/{agent_id}" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_XPANDER_API_KEY" \
  -d '{
    "use_oidc_pre_auth": true,
    "pre_auth_audiences": ["my-mcp-audience"],
    "oidc_pre_auth_token_mcp_audience": "my-mcp-audience"
  }'
```

Then send the user's access token on each call. Put it in `Authorization: Bearer` next to `x-api-key`, or in the body as `user_oidc_token`. Send it in one place. When both are present, they must carry the same token, or the call fails with 400 `user_oidc_token_conflict`.

```bash theme={"dark"}
curl -N -X POST "https://api.xpander.ai/v1/agents/{agent_id}/invoke/stream" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_ORG_DEFAULT_API_KEY" \
  -H "Authorization: Bearer USER_ACCESS_TOKEN" \
  -d '{
    "input": {
      "text": "List my open tickets",
      "user": {"id": "user-123", "email": "dana@example.com"}
    }
  }'
```

* The token is used only when `use_oidc_pre_auth` is on for the agent. Without it, `user_oidc_token` is ignored.
* xpander exchanges the token at your identity provider; it does not forward the raw token. If the exchange fails, the call fails.
* xpander does not check that the token belongs to `input.user`. Send the token of the same person you name.
* Exchanged tokens are cached for about 50 minutes per token and audience.

### Sign-in link in the stream

Send only `input.user` with a stable `id`. When the agent first needs an OAuth MCP connector that this user has not signed in to, the stream sends an `auth_event`. Its `data.type` is `login_required` and `data.data.url` is the sign-in link. Show the link to the user. After the user signs in, the run goes on, and later calls for the same `input.user.id` use the stored sign-in with no link.

```json theme={"dark"}
{"type": "auth_event", "data": {"type": "login_required", "data": {"url": "https://...", "server_url": "https://mcp.example.com/mcp", "server_name": "Example"}}}
```

A run with no `input.user.id` cannot start a sign-in, and the agent reports that MCP connector as not available. After a sign-in, the stream sends a second `auth_event` with `data.type` `token_ready`.

### A token per MCP connector

Send `user_tokens`: one entry per MCP connector, keyed by its `id` from [`GET /v1/agents/{agent_id}/tools`](https://docs.xpander.ai/api-reference/v1/agents/tools/list-agent-tools) (the items with `type` `mcp`). The value is a bearer token, or an object of headers to send as they are.

```bash theme={"dark"}
curl -X POST "https://api.xpander.ai/v1/agents/{agent_id}/invoke" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_ORG_DEFAULT_API_KEY" \
  -d '{
    "input": {
      "text": "Summarize my week",
      "user": {"id": "user-123", "email": "dana@example.com"}
    },
    "user_tokens": {
      "MCP_TOOL_ID_1": "USER_TOKEN_FOR_SERVER_1",
      "MCP_TOOL_ID_2": {"X-Api-Key": "USER_KEY_FOR_SERVER_2"}
    }
  }'
```

When the agent also has [one token per request](#one-token-per-request) turned on, the exchanged token takes the place of these entries.

## Credentials are not stored with the run

`user_tokens` and `user_oidc_token` stay with the live run only. They are removed from stored task history, exports and conversation views, so send them again on each call.

## API keys limited to some agents

In **Platform > Organization > Access > API keys**, a key other than the default key can be limited to named agents. A call with that key to any other agent is refused. Such a key always runs as its owner (see [Who the run acts as](#who-the-run-acts-as)), so use it for calls that act as one person, not for many end users.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.