# Get a token

Source: https://docs.musicnerd.net/api-reference/oauth/token

Get a token

## POST /api/oauth/token

Full OpenAPI specification: https://docs.musicnerd.net/spec/oauth.json

## Authentication

This operation's specification permits a request without authentication.

[Authentication guide](https://docs.musicnerd.net/authentication)

## Operation and referenced schemas

```json
{
  "openapi": "3.1.0",
  "info": {
    "title": "Music Nerd API: OAuth",
    "version": "1.0.0",
    "description": "How agents connect to the MCP server with OAuth: the discovery documents an MCP client reads, and the member's list of connected agents."
  },
  "servers": [
    {
      "url": "https://api.musicnerd.net",
      "description": "Production"
    },
    {
      "url": "https://musicnerd-api-staging.vercel.app",
      "description": "Staging (staging database)"
    }
  ],
  "paths": {
    "/api/oauth/token": {
      "post": {
        "summary": "Get a token",
        "operationId": "getOAuthToken",
        "security": [],
        "description": "Swaps an authorization code or a refresh token for a new access token and refresh token. Access tokens last five minutes. Refresh tokens rotate on every use and last until the user disconnects the agent; reusing one that was already used disconnects the agent, if it is the last one used or was used in the past 30 days.",
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "required": [
                  "grant_type",
                  "client_id"
                ],
                "properties": {
                  "grant_type": {
                    "type": "string",
                    "enum": [
                      "authorization_code",
                      "refresh_token"
                    ]
                  },
                  "client_id": {
                    "type": "string",
                    "description": "The `client_id` from registration."
                  },
                  "code": {
                    "type": "string",
                    "description": "For `authorization_code`: the code from the redirect. Single use, valid for 60 seconds."
                  },
                  "code_verifier": {
                    "type": "string",
                    "description": "For `authorization_code`: the PKCE verifier behind `code_challenge`."
                  },
                  "redirect_uri": {
                    "type": "string",
                    "description": "For `authorization_code`: the same `redirect_uri` as the authorization request."
                  },
                  "refresh_token": {
                    "type": "string",
                    "description": "For `refresh_token`: the latest refresh token. Single use."
                  }
                }
              },
              "example": {
                "grant_type": "authorization_code",
                "client_id": "2f6c1d0e-8a4b-4c47-9a51-3e0f7b9d2c18",
                "code": "mn_code_EXAMPLE",
                "code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
                "redirect_uri": "http://127.0.0.1:33418/callback"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "A new access token and refresh token, with `Cache-Control: no-store`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TokenResponse"
                },
                "example": {
                  "access_token": "mn_at_EXAMPLE",
                  "token_type": "Bearer",
                  "expires_in": 300,
                  "refresh_token": "mn_rt_EXAMPLE",
                  "scope": "mcp:tools"
                }
              }
            }
          },
          "400": {
            "description": "The code or refresh token is invalid, expired or already used, or the verifier, client or redirect URI doesn't match (`invalid_grant`); or the request is malformed (`invalid_request`, `unsupported_grant_type`). Errors use the standard OAuth shape.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                },
                "example": {
                  "error": "invalid_grant",
                  "error_description": "The code is invalid, expired or already used"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TokenResponse": {
        "type": "object",
        "required": [
          "access_token",
          "token_type",
          "expires_in",
          "refresh_token",
          "scope"
        ],
        "properties": {
          "access_token": {
            "type": "string",
            "description": "Send as `Authorization: Bearer` to `/mcp`."
          },
          "token_type": {
            "type": "string",
            "enum": [
              "Bearer"
            ]
          },
          "expires_in": {
            "type": "integer",
            "description": "Seconds until the access token expires: 300."
          },
          "refresh_token": {
            "type": "string",
            "description": "Single use; the next refresh returns a new one."
          },
          "scope": {
            "type": "string"
          }
        }
      },
      "OAuthError": {
        "type": "object",
        "description": "The standard OAuth error shape (RFC 6749), used by registration, authorization and the token endpoint instead of `{status, error}`.",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string",
            "description": "An OAuth error code, like `invalid_grant`."
          },
          "error_description": {
            "type": "string"
          }
        }
      }
    }
  }
}
```
