> For the complete documentation index, see [llms.txt](https://docs.chatlayer.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chatlayer.ai/~/changes/zWOY8fNiBBrqneKkuGvA/chatlayer-api/v2-api-authentication.md).

# V2 API Authentication

Find out about our new way of making authenticated calls to Chatlayer APIs.

We are moving away from long lived Access Tokens to authenticate against our APIs in favour of short-lived tokens that can be retrieved by following a Client Credentials flows using Client ID/Client Secret pairs.&#x20;

## Supported API Services

The following services support the V2 authentication:

* Machine Learning Services (NLP, Completions)
* Chatlayer GraphQL API

The Chatlayer V1 REST API will use the Access Token based authentication for the foreseeable future.

## Creating Access Credentials

In the Chatlayer App, go to Settings - Credentials and open the "Access Credentials" tab.

![](https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FPWTOOjW9iEyLnoaem2Ni%2Fimage.png?alt=media\&token=116c545c-1471-4f64-8cf1-4a39ed31422c)

Press the "Generate Credentials" button

![](https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2F2bCYYfEvHXMAX3b0Vh5x%2Fimage.png?alt=media\&token=f456243f-de46-4e47-be39-7ee1b69f4908)

Choose a fitting name and role for the new Access Credential.

Currently, the available roles are:

**Admin**\
Admins have access to all bots and can create or delete team members

**Member**\
Members can only access OPEN bots or RESTRICTED bots to which they have access. They cannot create or delete team members.

![](https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FqPSPvF3EyVfOb9QVqhr4%2Fimage.png?alt=media\&token=a90be3f5-434c-41fe-9bfe-29f29c523b88)

Once you press Create, your Access Credentials are immediately available.&#x20;

Make sure you've noted down the Client Secret - you can no longer retrieve it after you close the modal.

![](https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FXNC9QHKQxo3rAAJBeXil%2Fimage.png?alt=media\&token=b2652ed1-6419-4833-aef2-8ed82cde2875)

## Retrieving & using Access Tokens

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FoauGlR72wrzB2twUvp2t%2FGroup%206%20(3).png?alt=media&amp;token=25535ae5-4469-45c6-8c34-ab6696893263" alt=""><figcaption></figcaption></figure>

### 1 - Exchanging Access Credentials for Access Tokens&#x20;

Once you have your API Access Credentials (Client ID, Client Secret pair), you can request your API Access Token from the Chatlayer Authentication Service.&#x20;

Example of retrieving an Access Token through curl:

```bash
curl --location --request POST 'https://auth.prod.europe-west1.gc.chatlayer.ai/auth/realms/Chatlayer/protocol/openid-connect/token' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'grant_type=client_credentials' --data-urlencode 'client_id=your-client-id' --data-urlencode 'client_secret=your-client-secret'
```

The Authentication Service will return a JSON payload containing the `access_token`.&#x20;

```json
{
  "access_token": "eyJhbGciOi...",
  "expires_in": 300,
  "refresh_expires_in": 0,
  "token_type": "Bearer",
  "not-before-policy": 0,
  "scope": "offline_access profile public_api email"
}
```

{% hint style="info" %}
Access tokens are valid for 5 minutes, after which you will have to request a new token using the same url. \
You should automate your application to retrieve a new token within 5 minutes.
{% endhint %}

The Access Token can then be used to Authenticate against the Chatlayer API Services that support this way of authenticating.&#x20;

### 2 - Using Access Tokens

To use the Access Token, simply add it as part of the "Authorization" header of your requests to our API services. Make sure to prefix the token with the `Bearer` payload.

```http
Authorization: Bearer <<your_access_token>>
```

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.chatlayer.ai/~/changes/zWOY8fNiBBrqneKkuGvA/chatlayer-api/v2-api-authentication.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
