> ## 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.

# Exchange a token

> Exchanges an authorization code or a refresh token for a new access token and refresh token. Supports the `authorization_code` (with PKCE) and `refresh_token` grant types.

## Usage

Clients are public clients. Send `client_id` in the form body and do not send a client secret. PKCE proves the request comes from the client that started the flow.

Use the access token as `Authorization: Bearer <access_token>` on the public API and the MCP server. If `token_type` is `DPoP`, use `Authorization: DPoP <access_token>` with a `DPoP` proof header instead. The scoped access token endpoints are the exception and require an API key.

## Token lifetimes

| Token | Default lifetime | Setting |
| - | - | - |
| 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` |

## Refresh token rotation

Each refresh returns a new refresh token and deletes the one you sent. Store the new refresh token before using it. Sending an old refresh token returns `invalid_grant`. If several workers share one refresh token, make sure only one of them refreshes it at a time.


## OpenAPI

````yaml /api-reference/sourcebot-public.openapi.json post /api/ee/oauth/token
openapi: 3.0.3
info:
  title: Sourcebot Public API
  version: v6.1.2
  description: >-
    OpenAPI description for the public Sourcebot REST endpoints used for search,
    repository listing, and file browsing. API keys are the standard integration
    mechanism, and OAuth bearer tokens are EE-only.
servers: []
security:
  - bearerToken: []
  - apiKeyHeader: []
  - {}
tags:
  - name: Search & Navigation
    description: Code search and symbol navigation endpoints.
  - name: Connections
    description: Code host connection metadata.
  - name: Repositories
    description: Repository listing and metadata endpoints.
  - name: Git
    description: Git history, diff, and file content endpoints.
  - name: Scoped Access Tokens
    description: >-
      Mint and revoke short-lived credentials restricted to specific
      repositories.
  - name: System
    description: System health and version endpoints.
  - name: OAuth
    description: >-
      OAuth 2.0 authorization server for apps that call Sourcebot on behalf of a
      user.
  - name: Enterprise (EE)
    description: Enterprise endpoints for user management and audit logging.
paths:
  /api/ee/oauth/token:
    post:
      tags:
        - OAuth
      summary: Exchange a token
      description: >-
        Exchanges an authorization code or a refresh token for a new access
        token and refresh token. Supports the `authorization_code` (with PKCE)
        and `refresh_token` grant types.
      operationId: exchangeOAuthToken
      parameters:
        - schema:
            type: string
            description: >-
              RFC 9449 DPoP proof JWT, signed with `ES256`. When present, the
              issued tokens are bound to the proof key.
          required: false
          description: >-
            RFC 9449 DPoP proof JWT, signed with `ES256`. When present, the
            issued tokens are bound to the proof key.
          name: DPoP
          in: header
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/PublicOAuthTokenRequest'
      responses:
        '200':
          description: Tokens issued.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PublicOAuthTokenResponse'
        '400':
          description: >-
            The request is invalid. `error` is one of `invalid_request`,
            `invalid_grant`, `invalid_target`, `invalid_dpop_proof`, or
            `unsupported_grant_type`.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PublicOAuthError'
        '403':
          description: OAuth is not available on this instance (`access_denied`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PublicOAuthError'
      security: []
components:
  schemas:
    PublicOAuthTokenRequest:
      oneOf:
        - $ref: '#/components/schemas/PublicOAuthAuthorizationCodeTokenRequest'
        - $ref: '#/components/schemas/PublicOAuthRefreshTokenRequest'
      discriminator:
        propertyName: grant_type
        mapping:
          authorization_code: '#/components/schemas/PublicOAuthAuthorizationCodeTokenRequest'
          refresh_token: '#/components/schemas/PublicOAuthRefreshTokenRequest'
    PublicOAuthTokenResponse:
      type: object
      properties:
        access_token:
          type: string
          pattern: ^sboa_
          description: >-
            Opaque access token. When `token_type` is `Bearer`, send it as
            `Authorization: Bearer <token>`. When `token_type` is `DPoP`, send
            it as `Authorization: DPoP <token>` with a `DPoP` proof header.
        refresh_token:
          type: string
          pattern: ^sbor_
          description: >-
            Opaque refresh token. It replaces the refresh token used in this
            request.
        token_type:
          type: string
          enum:
            - Bearer
            - DPoP
          description: '`DPoP` when the token is bound to a DPoP key, otherwise `Bearer`.'
        expires_in:
          type: integer
          description: Access token lifetime in seconds.
        scope:
          type: string
          description: Space-separated granted scopes. Currently always empty.
      required:
        - access_token
        - refresh_token
        - token_type
        - expires_in
        - scope
    PublicOAuthError:
      type: object
      properties:
        error:
          type: string
          description: >-
            OAuth error code, such as `invalid_request`, `invalid_grant`, or
            `access_denied`.
        error_description:
          type: string
          description: Human-readable explanation of the error.
      required:
        - error
        - error_description
      description: >-
        Error response returned by the OAuth endpoints, as defined in RFC 6749
        Section 5.2.
    PublicOAuthAuthorizationCodeTokenRequest:
      type: object
      properties:
        grant_type:
          type: string
          enum:
            - authorization_code
        client_id:
          type: string
        code:
          type: string
          description: Authorization code from the redirect. Codes are single use.
        redirect_uri:
          type: string
          format: uri
          description: Must equal the `redirect_uri` sent to the authorization endpoint.
        code_verifier:
          type: string
          description: PKCE code verifier that produced the `code_challenge`.
        resource:
          type: string
          format: uri
          description: >-
            RFC 8707 resource indicator. Required when the authorization code or
            refresh token was bound to a resource, and must match it.
      required:
        - grant_type
        - client_id
        - code
        - redirect_uri
        - code_verifier
      description: Authorization code
    PublicOAuthRefreshTokenRequest:
      type: object
      properties:
        grant_type:
          type: string
          enum:
            - refresh_token
        client_id:
          type: string
        refresh_token:
          type: string
          pattern: ^sbor_
          description: The most recent refresh token issued to the client.
        resource:
          type: string
          format: uri
          description: >-
            RFC 8707 resource indicator. Required when the authorization code or
            refresh token was bound to a resource, and must match it.
      required:
        - grant_type
        - client_id
        - refresh_token
      description: Refresh token
  securitySchemes:
    bearerToken:
      type: http
      scheme: bearer
      description: >-
        Bearer authentication header of the form `Bearer <token>`. The token may
        be a Sourcebot API key, OAuth access token, or scoped access token,
        subject to endpoint requirements.
    apiKeyHeader:
      type: apiKey
      in: header
      name: X-Sourcebot-Api-Key
      description: >-
        Header of the form `X-Sourcebot-Api-Key: <token>`, where `<token>` is
        your API key.

````

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