# OAuth Token API

Access Tokens can be generated by calling the Token API endpoint and passing Client ID and Client Secret of an OAuth app.

## Token API

`POST https://oauth.twilio.com/v2/token`

**Encoding type**:`application/x-www-form-urlencoded`

#### Request body parameters

| Parameter       | Type   | Required | Description                                                                                                            |
| --------------- | ------ | -------- | ---------------------------------------------------------------------------------------------------------------------- |
| `client_id`     | string | yes      | The unique identifier of an OAuth app                                                                                  |
| `client_secret` | string | yes      | The confidential secret associated with the client ID                                                                  |
| `grant_type`    | string | yes      | Specify the OAuth grant type. Right now we only support Client Credentials so this must be set to `client_credentials` |

#### cURL Request

```bash
curl --location 'https://oauth.twilio.com/v2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id={ClientID}' \
--data-urlencode 'client_secret={ClientSecret}' \
--data-urlencode 'grant_type=client_credentials'
```

#### Sucess response

**Status**: `200 OK`

**Body(JSON)**:

```json
{
    "access_token": "{AccessToken}",
    "id_token": null,
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": null
}
```

**Response Fields**

| Parameter      | Type    | Description                                         |
| -------------- | ------- | --------------------------------------------------- |
| `access_token` | string  | The Access Token issued by the authorization server |
| `token_type`   | string  | The type of token (typically `Bearer`)              |
| `expires_in`   | integer | The token lifetime in seconds                       |

## API resource call examples

Use the `access_token` generated as response from Token API endpoint as bearer token in the `Authorization` header for Twilio API requests.

**cURL example to `GET` list of Messages of an account**

```bash
curl --location 'https://api.twilio.com/2010-04-01/Accounts/<AccountSID>/Messages.json' \
--header 'Authorization: Bearer {AccessToken}'
```

## Server-side SDK support

OAuth support for Twilio APIs is available in all server-side SDKs. Here are details of the versions and examples:

* Java SDK version 10.6.0 and above:
  * [README](https://github.com/twilio/twilio-java/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-java/blob/main/examples/PublicOAuthExample.md).
* C# SDK version 7.6.0 and above:
  * [README](https://github.com/twilio/twilio-csharp/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-csharp/blob/main/examples/PublicOAuthAuthentication.md).
* Node SDK version 5.4.0 and above:
  * [README](https://github.com/twilio/twilio-node/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-node/blob/main/examples/public_oauth.js).
* Python SDK version 9.4.1 and above:
  * [README](https://github.com/twilio/twilio-python/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-python/blob/main/examples/public_oauth.py).
* Ruby SDK version 7.4.0 and above:
  * [README](https://github.com/twilio/twilio-ruby/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-ruby/blob/main/examples/public_oauth.rb).
* PHP SDK version 8.5.0 and above:
  * [README](https://github.com/twilio/twilio-php/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-php/blob/main/example/public_oauth_example.php).
* Go SDK version 1.25.1 and above:
  * [README](https://github.com/twilio/twilio-go/blob/main/README.md)
  * [Public OAuth example](https://github.com/twilio/twilio-go/blob/main/advanced-examples/public-oauth.md).
