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

# Exchange a code for a key

> Step 3 of one-click connect. Trade the `code` Sonder sent back to your `redirect_uri` for an API key. Codes last 10 minutes and work once. Authenticate with your client ID and secret (HTTP Basic or in the body). Errors follow OAuth 2.0: `{ "error", "error_description" }`.



## OpenAPI

````yaml /api-reference/openapi.json post /oauth/token
openapi: 3.1.0
info:
  title: Sonder API
  version: 1.0.0
  description: >-
    Read an author's Sonder videos, views, viral days and view milestones. Every
    request uses an API key the author creates in Sonder, and sees that author's
    data only.
servers:
  - url: https://api.yousonder.com/v1
    description: Production
  - url: https://sonder-server-production.up.railway.app/v1
    description: Production (direct address)
security:
  - bearerAuth: []
tags:
  - name: Account
    description: Who the key belongs to.
  - name: Books
    description: The author's books.
  - name: Videos
    description: Posted videos with views, viral status and milestones.
  - name: Stats
    description: Day-by-day views.
  - name: Events
    description: Viral and milestone moments, for polling.
  - name: Connect
    description: One-click connect (OAuth 2.0).
paths:
  /oauth/token:
    post:
      tags:
        - Connect
      summary: Exchange a code for a key
      description: >-
        Step 3 of one-click connect. Trade the `code` Sonder sent back to your
        `redirect_uri` for an API key. Codes last 10 minutes and work once.
        Authenticate with your client ID and secret (HTTP Basic or in the body).
        Errors follow OAuth 2.0: `{ "error", "error_description" }`.
      operationId: oauthToken
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/TokenRequest'
          application/json:
            schema:
              $ref: '#/components/schemas/TokenRequest'
      responses:
        '200':
          description: An API key for this author.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TokenResponse'
              example:
                access_token: sonder_live_q8Zr2mN4xT7vB1cK9pL3sD6fG0hJ5wYe
                token_type: Bearer
                scope: read
                account:
                  id: 2c6f9a10-3d4b-4e8f-9a1b-7c5d3e2f1a09
                  email: jane@example.com
        '400':
          description: The code is invalid, expired, used, or does not match.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OAuthError'
              example:
                error: invalid_grant
                error_description: That code has expired (codes last 10 minutes).
        '401':
          description: Unknown client or wrong client secret.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OAuthError'
              example:
                error: invalid_client
                error_description: Unknown client or wrong client secret.
      security: []
components:
  schemas:
    TokenRequest:
      type: object
      required:
        - grant_type
        - code
        - redirect_uri
      properties:
        grant_type:
          type: string
          const: authorization_code
        code:
          type: string
          description: The `code` Sonder sent to your redirect_uri.
        redirect_uri:
          type: string
          description: Exactly the one you sent the author with.
        client_id:
          type: string
          description: Or send it with HTTP Basic auth.
        client_secret:
          type: string
          description: Or send it with HTTP Basic auth.
        code_verifier:
          type: string
          description: Required if you sent a code_challenge (PKCE).
    TokenResponse:
      type: object
      required:
        - access_token
        - token_type
        - scope
        - account
      properties:
        access_token:
          type: string
          description: >-
            An API key. Store it like a password; it does not expire, and the
            author can revoke it in Sonder.
        token_type:
          type: string
          const: Bearer
        scope:
          type: string
          const: read
        account:
          type: object
          required:
            - id
            - email
          properties:
            id:
              type: string
              format: uuid
            email:
              type:
                - string
                - 'null'
    OAuthError:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          enum:
            - invalid_request
            - invalid_client
            - invalid_grant
            - unsupported_grant_type
            - server_error
        error_description:
          type: string
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: An API key from Sonder → Settings → API keys, like `sonder_live_…`.

````