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

# OAuth 2.0

Sourcebot supports OAuth 2.0 for apps that call Sourcebot on behalf of a Sourcebot user. Use it when you are building an integration where each user signs in with their own Sourcebot account, instead of sharing one API key.

Sourcebot uses the **authorization code grant with PKCE** and issues refresh tokens. Every request made with an access token acts as the user who approved it, with the same [role and permissions](https://docs.sourcebot.dev/docs/configuration/auth/roles-and-permissions.md). When [permission syncing](https://docs.sourcebot.dev/docs/features/permission-syncing.md) is configured, the app only sees repositories that user can access.

In the examples below, replace `https://your-sourcebot-instance.com` with the URL of your Sourcebot deployment.

## Register an OAuth application

Register your app with [dynamic client registration](https://docs.sourcebot.dev/api-reference/oauth/register-a-client.md). Registration does not need authentication. Do it once and store the returned `client_id`.

```bash theme={null}
curl -X POST https://your-sourcebot-instance.com/api/ee/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "My App",
    "redirect_uris": ["https://myapp.example.com/oauth/callback"],
    "logo_uri": "https://myapp.example.com/logo.png"
  }'
```

```json theme={null}
{
  "client_id": "cm9x2k1ab0000xyz",
  "client_name": "My App",
  "logo_uri": "https://myapp.example.com/logo.png",
  "redirect_uris": ["https://myapp.example.com/oauth/callback"]
}
```

The `client_name` and `logo_uri` are shown to users on the consent screen.

> [!NOTE]
>
> Sourcebot does not issue client secrets. Every client is a public client and proves its identity with PKCE. Wildcard redirect URIs are not allowed.

## Redirect the user to Sourcebot

Send the user's browser to the authorization endpoint. If they are not signed in, Sourcebot asks them to sign in first. They then approve or deny your app on the consent screen.

```http theme={null}
GET https://your-sourcebot-instance.com/oauth/authorize
```

| Parameter | Description |
| - | - |
| `response_type=code` | (required) |
| `client_id` | (required) The `client_id` returned at registration. |
| `redirect_uri` | (required) Must exactly match one of your registered redirect URIs. |
| `code_challenge` | (required) `BASE64URL(SHA256(code_verifier))`. Generate a random `code_verifier` for each authorization request and keep it for the token exchange. |
| `code_challenge_method=S256` | (optional, recommended) Only `S256` is supported. |
| `state` | (optional, recommended) A random value that Sourcebot returns unchanged. Use it to protect against CSRF. |

Do not send a `scope` parameter. Sourcebot does not define any scopes yet, and a request with a scope fails with `invalid_scope`.

### Example

```http theme={null}
GET https://your-sourcebot-instance.com/oauth/authorize
  ?response_type=code
  &client_id=cm9x2k1ab0000xyz
  &redirect_uri=https%3A%2F%2Fmyapp.example.com%2Foauth%2Fcallback
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256
  &state=SECURE_RANDOM
```

## Handle the redirect

When the user approves, Sourcebot redirects to your `redirect_uri` with a `code` and the `state` you sent. Always check that `state` matches the value you sent before you continue.

```http theme={null}
https://myapp.example.com/oauth/callback?code=AUTHORIZATION_CODE&state=SECURE_RANDOM
```

If the user denies access, the redirect carries an error instead:

```http theme={null}
https://myapp.example.com/oauth/callback?error=access_denied&error_description=The+user+denied+the+authorization+request.&state=SECURE_RANDOM
```

The authorization code can be used once and expires after 10 minutes.

## Exchange the code for tokens

Exchange the code and the PKCE `code_verifier` for an access token and a refresh token.

```http theme={null}
POST https://your-sourcebot-instance.com/api/ee/oauth/token
Content-Type: application/x-www-form-urlencoded
```

| Parameter | Description |
| - | - |
| `grant_type=authorization_code` | (required) |
| `code` | (required) The code from the redirect. |
| `redirect_uri` | (required) The same `redirect_uri` you sent to the authorization endpoint. |
| `client_id` | (required) |
| `code_verifier` | (required) The verifier you used to create the `code_challenge`. |

### Example

```bash theme={null}
curl -X POST https://your-sourcebot-instance.com/api/ee/oauth/token \
  -d grant_type=authorization_code \
  -d code=AUTHORIZATION_CODE \
  -d redirect_uri=https://myapp.example.com/oauth/callback \
  -d client_id=cm9x2k1ab0000xyz \
  -d code_verifier=CODE_VERIFIER
```

### Response

```json theme={null}
{
  "access_token": "sboa_...",
  "refresh_token": "sbor_...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": ""
}
```

## Make API requests

Send the access token as a Bearer token in the `Authorization` header, the same way you would send an [API key](https://docs.sourcebot.dev/docs/api-reference/authentication.md). Access tokens work with the public API and the [MCP server](https://docs.sourcebot.dev/docs/mcp/overview.md). The [scoped access token](https://docs.sourcebot.dev/docs/api-reference/authentication.md#using-a-scoped-access-token) endpoints are the exception and require an API key.

```bash theme={null}
curl -X POST https://your-sourcebot-instance.com/api/search \
  -H "Authorization: Bearer sboa_..." \
  -H "Content-Type: application/json" \
  -d '{"query": "hello world", "matches": 10}'
```

## Refresh an access token

When the access token expires, exchange the refresh token for a new access token.

```http theme={null}
POST https://your-sourcebot-instance.com/api/ee/oauth/token
Content-Type: application/x-www-form-urlencoded
```

| Parameter | Description |
| - | - |
| `grant_type=refresh_token` | (required) |
| `refresh_token` | (required) The most recent refresh token you received. |
| `client_id` | (required) |

The response has the same shape as the code exchange, including a new `refresh_token`.

> [!WARNING]
>
> Refresh tokens rotate. Each refresh returns a new refresh token and invalidates the one you sent, so store the new one before you use it. Sending an old refresh token fails with `invalid_grant`, and the user has to authorize your app again. If several processes share a token, make sure only one refreshes it at a time.

## Revoke a token

Revoke a token when the user disconnects your app. Revoking an access token does not revoke its refresh token, so revoke both. Revoking the current pair does not revoke access tokens issued before the most recent refresh. Those tokens remain valid until they expire.

```http theme={null}
POST https://your-sourcebot-instance.com/api/ee/oauth/revoke
Content-Type: application/x-www-form-urlencoded
```

| Parameter | Description |
| - | - |
| `token` | (required) The access token (`sboa_...`) or refresh token (`sbor_...`) to revoke. |
| `client_id` | (optional) Your `client_id`. Used to attribute the revocation in the [audit log](https://docs.sourcebot.dev/docs/configuration/audit-logs.md). |

The endpoint always returns `200`, even if the token does not exist.

Users can also see and disconnect authorized apps in **Settings → MCP → Connected clients**.

## Token lifetimes

| Token | Default lifetime | Environment variable |
| - | - | - |
| Authorization code | 10 minutes | `OAUTH_AUTHORIZATION_CODE_TTL_SECONDS` |
| Access token | 1 hour | `OAUTH_ACCESS_TOKEN_TTL_SECONDS` |
| Refresh token | 90 days | `OAUTH_REFRESH_TOKEN_TTL_SECONDS` |

Administrators can change these with [environment variables](https://docs.sourcebot.dev/docs/configuration/environment-variables.md).

## Discovery

Sourcebot publishes [authorization server metadata](https://docs.sourcebot.dev/api-reference/oauth/get-authorization-server-metadata.md) (RFC 8414) at `/.well-known/oauth-authorization-server`. Clients that support discovery can read the endpoint URLs from it instead of hard-coding them.


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