# Exchange code for tokens

> Exchange an authorization code for an access token and refresh token.

Source: https://docs.mycrmsim.com/docs/authentication/token-exchange

```http
POST https://backend.mysmsgateway.app/api/v1/third_party_integrations/oauth/token
```

- Category: Authorization / OAuth 2.0
- Auth: OAuth client credentials

Server-to-server. Send the code your callback received along with your client_id + client_secret. Returns both tokens — store them in your database, keyed by the installing user. Codes are single-use and expire 10 minutes after consent: a second exchange with the same code returns an error indicating that the code has already been used. If the exchange times out or returns a 5xx, retry it once; if the retry reports the code as already used, the first attempt was processed and no tokens can be recovered — send the user through the install URL again. The token endpoints are not covered by the per-installation API rate limits and do not return 429.

## Request body

```json
{
  "grant_type": "authorization_code",
  "code": "4f8a7e1d2c9b3a5f6e0d1c2b7a8f9e6d",
  "client_id": "<YOUR CLIENT_ID>",
  "client_secret": "<YOUR CLIENT_SECRET>",
  "redirect_uri": "https://your-app.com/oauth/callback"
}
```

## Response 200

Success — tokens issued

```json
{
  "success": true,
  "data": {
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "token_type": "Bearer",
    "expires_in": 86400,
    "scopes": [
      "contacts.read",
      "contacts.write"
    ]
  }
}
```

## Response 400

Invalid or expired authorization code

```json
{
  "success": false,
  "message": "Invalid or expired authorization code"
}
```

## Response 400

Code already used (single-use enforcement)

```json
{
  "success": false,
  "message": "Authorization code has already been used. Re-start the install to get a fresh code."
}
```
