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

# Customize Tokens Using Hooks with Client Credentials Flow

> Learn how to use Hooks to change scopes and add custom claims in the access token obtained using the Client Credentials Flow.

You can change scopes and add custom claims in the tokens issued through the [Client Credentials Flow](/docs/docs/get-started/authentication-and-authorization-flow/client-credentials-flow) by adding [Hooks](/docs/docs/customize/hooks).

Hooks allow you to customize the behavior of Auth0 using Node.js code. They are secure, self-contained functions associated with specific extensibility points of the Auth0 platform (like the Client Credentials flow). Auth0 invokes the Hooks at runtime to execute your custom logic.

You can manage Hooks using the Auth0 Dashboard (Auth0 Dashboard: Auth0's main product to configure your services.) or the Management API (Auth0 Dashboard: Auth0's main product to configure your services.).

> [!WARNING]
>
> Although you may create multiple hooks for any given extensibility point, each extensibility point may have only one enabled hook at a time. Any subsequent hooks you create for that extensibility point are automatically disabled, so you must explicitly enable them. The enabled hook will be executed for all applications and APIs.

## Prerequisites

Before beginning this tutorial, you must:

* [Register your API with Auth0](/docs/docs/get-started/auth0-overview/set-up-apis)

  * [Add appropriate API permissions](/docs/docs/get-started/apis/add-api-permissions)
* [Register the M2M Application with Auth0](/docs/docs/get-started/auth0-overview/create-applications/machine-to-machine-apps).

  * Select an **Application Type** of **Machine to Machine Applications**.
  * Choose your previously-registered API.
  * Authorize the M2M Application to call your API.

## Steps

1. **Create Hook**: Create a hook that will customize your token.
2. **Test Hook**: Test your new hook by running a Client Credentials Flow and decoding the access token.

### Create a Hook

In this example, you will:

* add an arbitrary claim (`https://foo.com/claim`) to the [Access Token](/docs/docs/secure/tokens/access-tokens)
* add an extra permission to your configured API

Create a Hook to customize your token. When asked to choose an extensibility point, select `Client Credentials Exchange`, and add the following code in the editor:

```js lines theme={null}
module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token['https://foo.com/claim'] = 'bar';
  access_token.scope = scope;
  access_token.scope.push('extra');
  cb(null, access_token);
};
```

> [!WARNING]
>
> Auth0 returns profile information in a structured claim format as defined by the [OpenID Connect (OIDC) specification](https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims). This means that custom claims added to ID tokens or access tokens must [conform to guidelines and restrictions](/docs/docs/secure/tokens/json-web-tokens/create-custom-claims) to avoid possible collisions.

### Test your Hook

To test the hook you just created you need to run a Client Credentials exchange, get the Access Token (Access Token: Authorization credential, in the form of an opaque string or JWT, used to access an API.), decode it, and review its contents.

#### Get token

To get a token, make a `POST` call to the [Client Credentials Flow endpoint](/docs/docs/api/authentication#client-credentials-flow). Be sure to replace CLIENT\_ID, CLIENT\_SECRET, and API\_IDENTIFIER placeholder values with your application's Client ID (Client ID: Identification value given to your registered resource from Auth0.), your application's Client Secret (Client Secret: Secret used by a client (application) to authenticate with the Authorization Server; it should be known to only the client and the Authorization Server and must be sufficiently random to not be guessable.), and your API's Identifier, respectively. You can find the Client ID and Client Secret in your [Application](https://manage.auth0.com/#/applications) settings and the API Identifier in your [API](https://manage.auth0.com/#/apis) settings.

A successful response will include:

* an `access_token`,
* its expiration time in seconds (`expires_in`),
* the token's type set as `Bearer` (`token_type`), and
* an `extra` permission (`scope`) (which was added by your Hook)

```json lines theme={null}
HTTP/1.1 200 OK
Content-Type: application/json
{
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5ESTFNa05DTVRGQlJrVTRORVF6UXpFMk1qZEVNVVEzT1VORk5ESTVSVU5GUXpnM1FrRTFNdyJ9.eyJpc3MiOiJodHRwczovL2RlbW8tYWNjb3VudC5hdXRoMC3jb20vIiwic3ViIjoic0FRSlFpQmYxREw0c2lqSVZCb2pFRUZvcmRoa0o4WUNAY2xpZW50cyIsImF1ZCI6ImRlbW8tYWNjb3VudC5hcGkiLCJleHAiOjE0ODc3NjU8NjYsImlhdCI6MTQ4NzY3OTI2Niwic2NvcGUiOiJyZWFkOmRhdGEgZXh0cmEiLCJodHRwczovL2Zvby5jb20vY2xhaW0iOiKoPXIifQ.da-48mHY_7esfLZpvHWWL8sIH1j_2mUYAB49c-B472lCdsNFvpaLoq6OKQyhnqk9_aW_Xhfkusos3FECTrLFvf-qwQK70QtwbkbVye_IuPSTAYdQ2T-XTzGDm9Nmmy5Iwl9rNYLxVs2OoCdfpVMyda0OaI0AfHBgEdKWluTP67OOnV_dF3KpuwtK3dPKWTCo2j9VCa7X1I4h0CNuM79DHhY2wO7sL8WBej7BSNA3N2TUsp_YTWWfrvsr_vVhJf-32G7w_12ms_PNFUwj2C30ZZIPWc-uEkDztyMLdI-lu9q9TLrLdr0dOhfrtfkdeJx4pUSiHdJHf42kg7UAVK6JcA",
  "expires_in": 86400,
  "scope": "extra",
  "token_type": "Bearer"
}
```

#### Decode token

The easiest way to decode the access token and review its contents is to use the [JWT.io Debugger](https://jwt.io/#debugger-io).

Copy your access token and paste it into the editor. The JWT (JSON Web Token (JWT): Standard ID Token format (and often Access Token format) used to represent claims securely between two parties.) is decoded automatically and its contents are displayed.

Note that the last two items of the **Payload** have both been set by your hook:

* `"scope": "extra"`
* `"https://foo.com/claim": "bar"`

## Learn more

* [Auth0 Hooks](/docs/docs/customize/hooks)
* [Client Credentials Flow](/docs/docs/get-started/authentication-and-authorization-flow/client-credentials-flow)
* [Call Your API Using the Client Credentials Flow](/docs/docs/get-started/authentication-and-authorization-flow/client-credentials-flow/call-your-api-using-the-client-credentials-flow)
