Agent Authorization

Introduction

To call LiveChat APIs, you need to authorize your calls with an access token. This document describes three authorization flows that let you acquire a token:

  • Personal Access Token
  • Implicit flow
  • Authorization code grant

With the acquired token, you can the following APIs on behalf of an agent:

  • the Agent Chat API
  • the Configuration API
  • LiveChat Accounts API
  • HelpDesk API
  • Reports API

To make calls to Customer Chat API, you need a different access_token. For that, see Customer Authorization.

Postman collection

You can find all the requests from the Agent and Customer authorization flows in Postman. Remember to replace sample parameters with your own.

Run in Postman

Personal Access Tokens

Unlike other authorization flows that let you acquire a token for your app, Personal Access Tokens (PATs) are generated per Agent. PATs are great when you want to make a quick call to the LiveChat API or test out the app you're working on.

To sign a request with a Personal Access Token, you need to:

  1. Create a PAT in Developer Console.
  2. Use your account_id and PAT in the Basic authentication scheme to send a request.

Step 1: Generate a PAT

Start in Developer Console. Then, go to Tools -> Personal Access Tokens and create a new token together with necessary scopes. You won't be able to change those scopes once you create the token.

LiveChat Personal Access Tokens

Step 2: Use your credentials to send the request

Personal Access Tokens use the Basic authentication scheme. For that reason, you need a username and a password. Please note that with Basic Auth, you need to encode your credentials using base64.

Use:

Once you have your credentials, you can call the Agent Chat API, Configuration API, LiveChat Accounts API, or HelpDesk API. Other LiveChat APIs don't support PAT authorization.

Implicit grant

Implicit grant is an authorization flow recommended for JavaScript web apps, but it works for both types: JavaScript and server-side apps.

To set up your own web app, you must define the URL of the app and the list of scopes. Scopes determine which parts of a LiveChat user's account your app will have access to. A LiveChat customer who enters your app URL will be asked to enter their login and password and grant the access for your app. Then, the user is redirected to your app with access_token included in the URL.

There are a few steps in the process:

  • Step 1: Create an app in Developer Console and configure the Authorization building block
  • Step 2: Redirect the users of your app to the LiveChat OAuth Server
  • Step 3: Get an access token from the URL
  • Step 4: Use the token in API calls
Implicit Grant LiveChat

Step 1: Create an app

If you don't have an app yet, start by creating one in Developer Console. The app is a LiveChat OAuth 2.1 Client with its Id and Secret. You need to configure the Authorization building block of your application. See the guide to learn how to do that. Since this is the Implicit grant authorization flow, you won't use the Client Secret of your app. Feel free to skip the part about Client Secret in the guide; the rest applies without any change.

Step 2: Redirect users to LiveChat OAuth Server

When users run your app, they should be redirected to the LiveChat OAuth Server, which can be found under this URL:

https://accounts.livechat.com/
Request
ParameterRequiredDescription
response_typeyesValue: token
client_idyesClient Id from Developer Console (Authorization block)
redirect_uriyesOne of the URIs defined in the Authorization block during app configuration. The LiveChat OAuth Server will redirect the user back to this URI after successful authorization.
statenoAny value that might be useful to your application. It's strongly recommended to include an anti-forgery token to mitigate the cross-site request forgery.
promptnoValue: consent. For testing purposes. It forces the app to ask for access to certain resources. It’s necessary for you to test the app as if you were a user who installs the app from Marketplace.
EXAMPLE REDIRECTION TO LIVECHAT OAUTH SERVER
Copied!
https://accounts.livechat.com/
  ?response_type=token
  &client_id=9cbf3a968289727cb3cdfe83ab1d9836
  &redirect_uri=https%3A%2F%2Fmy-application.com
  &state=i8XNjC4b8KVok4uw5RftR38Wgp2BFwql

At this point, the app should ask the user to allow it to access certain resources and perform certain actions. The list of resources and actions is automatically created based on the scopes selected for your app in Developer Console.

LiveChat Access Grant Request Modal

Keep in mind that as the app author, you won't see this screen. Use prompt:consent to verify this step from the user perspective. This step is also omitted for private web apps installed by Agents from the same license.

Step 3: Get an access token from the URL

After a user authorizes the app by clicking Allow, they are redirected back to your application (to the Redirect URI you specified in Developer Console). The URL includes a number of parameters, including the access_token.

Response
ParameterDescription
access_tokenThe token you can use to call LiveChat APIs on behalf of the user.
expires_inThe number of seconds the access_token will be valid; 28800 sec by default. When it expires, you will need to repeat the authorization process to get the new access_token.
token_typeValue: Bearer
stateThe value of the state param that you passed to LiveChat OAuth Server in redirection.

πŸ’‘ Got stuck? See common Errors.

EXAMPLE REDIRECTION BACK TO THE APP
Copied!
https://my-application.com/
  #access_token=dal%3Atest_DQTRHGbZCFkAoss4Q
  &token_type=Bearer
  &expires_in=28800
  &state=i8XNjC4b8KVok4uw5RftR38Wgp2BFwql

Step 4: Use the token in API calls

Once you extract the token from the URL, your app can use it to sign requests to the LiveChat API. Your application should store the access_token in localStorage or a cookie until it expires. Caching the token prevents you from redirecting the user to LiveChat OAuth Server every time they visit your app.

Example implementation

This sample web app makes a call to Agent Chat API to return the list of customers, which is then logged in the console. The application uses the Implicit grant to get an access token.

index.html
Copied!
<!DOCTYPE html>
<html>

<body>
  <script src="/get_customers.js"></script>
  ​
  <script>
    function getHashParam(key) {
      var params = location.hash.substring(1).split("&");
      var value = params.find(function (item) {
        return item.split("=")[0] === key;
      });
      return value ? value.split("=")[1] : "";
    }

    const clientId = "bb9e5b2f1ab480e4a715977b7b1b4279"; // Client Id of your app
    const redirectUri = "https://get-customers-app.samplehosting.com/"; // URL of your app
    const accessToken = decodeURIComponent(getHashParam("access_token"));

    if (accessToken) {
      get_customers();
    } else {
      location.href =
        "https://accounts.livechat.com/" +
        "?response_type=token" +
        "&client_id=" +
        clientId +
        "&redirect_uri=" +
        redirectUri;
    }
  </script>
</body>

</html>
get_customers.js
Copied!
const get_customers = () => {
  fetch('https://api.livechatinc.com/v3.1/agent/action/get_customers', {
    method: 'post',
    headers: {
      'Content-type': 'application/json',
      'Authorization': 'Bearer ' + accessToken,
    },
    body: JSON.stringify({})
  })
    .then((response) => response.json())
    .then((data) => {
      console.log('Request succeeded with JSON response', data);
    })
    .catch((error) => {
      console.log('Request failed', error);
    });
}

To make it work, run this app on localhost or deploy it to Firebase to host it. Update index.html with your own redirectUri (link to your app) and clientId. Make sure to use the exact same Redirect URI in Developer Console. Also, to use the Get Customers method, your app needs the customers:ro scope, which you should select in Developer Console. When everything is ready, install the app privately for your license.

Authorization code grant

Authorization code grant flow is recommended for server-side apps. Unlike web apps, they can store confidential info, such as Client Secret, on a server without ever exposing it. When a user runs your app, they are redirected to the LiveChat OAuth Server only once. After successful authorization, the user is redirected back to your app along with a single-use authorization code. Then, your application exchanges the code for an access token and a refresh token using the Client Secret. From now on, the app can regenerate new access tokens without any action required from the user.

This flow is very similar to Implicit grant, but contains one additional step of exchanging the code for an access token.

The steps are as follows:

  • Step 1: Create an app in Developer Console and configure the Authorization building block
  • Step 2: Redirect the users of your app to the LiveChat OAuth Server
  • Step 3: Get a code from the URL
  • Step 4: Exchange the code for an access token
  • Step 5: Use the token in API calls
LiveChat Public Server-Side Apps

PKCE extension

OAuth 2.1 introduces the PKCE (Proof Key for Code Exchange) extension for the Authorization code grant flow. It allows web applications to use the Authorization code grant flow, and also, enables the possibility to use custom schema redirects, like: my_app: // (especially useful with native applications).

The Authorization code grant flow with PKCE is recommended for both web apps and server-side apps. Since web app clients can't store Client Secrets securely, their Authorization code grant flow with PKCE differs from the one for server-side apps.

  • Web apps - Client Secret cannot be used, so it's not mandatory; refresh tokens rotate.
  • Server-side apps - Client Secret is mandatory to exchange a code for an access token and to refresh a token; refresh tokens don't rotate.

How does the Authorization code grant flow work with PKCE?

  • The client generates code_verifier from the following characters: [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~". It's between 43 and 128 characters long.
  • The client generates code_challenge with code_challenge_method as follows:
    • plain - code_challenge = code_verifier, when no code_challenge_method is present, then plain is assumed.
    • S256 - code_challenge = b64(s256(ascii(code_verifier))) where b64 is Base64 URL encoding and s256 is the SHA256 hash function.
  • The client sends code_challenge in the authorization request.
  • The server responds with the code.
  • The client sends code_verifier during the exchane of the for an access token.
  • The server performs an additional validation for code_challenge and code_verifier. Upon successful validation, it returns the access token.

πŸ’‘ To see code samples with PKCE parameters, use the dropdown list.

LiveChat code samples with PKCE

Step 1: Create an app

If you don't have an app yet, start by creating one in Developer Console. The app is a LiveChat OAuth 2.1 Client with its Id and Secret. You need to configure the Authorization building block of your application. See the guide to learn how to do that.

Step 2: Redirect users to LiveChat OAuth Server

When users run your app, they should be redirected to the LiveChat OAuth Server, which can be found under this URL:

https://accounts.livechat.com/
Request
ParameterRequiredRequired with PKCEDescription
response_typeyesyesValue: code
client_idyesyesClient Id from Developer Console (Authorization block)
redirect_uriyesyesOne of the URIs defined in the Authorization block during app configuration. The LiveChat OAuth Server will redirect the user back to this URI after successful authorization.
statenonoAny value that might be useful to your application. It's strongly recommended to include an anti-forgery token to mitigate the cross-site request forgery.
code_challengenoyesA string between 43 and 128 characters long.
code_challenge_method-noPossible values: s256 or plain; default: plain.
EXAMPLE REDIRECTION
Copied!
https://accounts.livechat.com/
  ?response_type=code
  &client_id=9cbf3a968289727cb3cdfe83ab1d9836
  &redirect_uri=https%3A%2F%2Fmy-application.com
  &state=i8XNjC4b8KVok4uw5RftR38Wgp2BFwql

At this point, the app should ask the user to allow it to access certain resources and perform certain actions. The list of resources and actions is automatically created based on the scopes selected for your app in Developer Console.

Access Grant Request

Keep in mind that as the app author, you won't see this screen. Use prompt:consent to verify this step from the user perspective. This step is also omitted for private server-side apps installed by Agents from the same license.

Step 3: Acquire the code

After a user authorizes the app by clicking Allow, they are redirected back to your application (to the Redirect URI you specified in Developer Console). The URL includes a number o parameters, including code.

Request
ParameterNotes
codeA single-use code you need to exchange it for an access token. It's only valid for a few minutes.
stateThe value of the state param that you passed to LiveChat OAuth Server in redirection.
EXAMPLE REDIRECTION BACK TO THE APP
Copied!
https://my-application.com/
  ?code=test_7W91a-oMsCeLvIaQm6bTrgtp7
  &state=i8XNjC4b8KVok4uw5RftR38Wgp2BFwql

Step 4: Exchange code for access token and refresh token

To exchange the code for an access_token and a refresh_token, you need to make an HTTP POST request to the following URL:

https://accounts.livechat.com/v2/token
Request
ParameterRequiredRequired with PKCE server side appRequired with PKCE javascript appDescription
grant_typeyesyesyesValue: authorization_code
codeyesyesyesThe authorization code returned from the request in the previous step.
client_idyesyesyesClient Id from Developer Console (Authorization block)
client_secretyesyesnoUnique, confidential identifier of your app from Developer Console (Authorization block).
redirect_uriyesyesyesThe URI defined in Step 2. The LiveChat OAuth Server will redirect the user back to this URL after successful authorization.
code_verifiernoyesyesThe previously generated code_verifier.
Response

The response is a JSON with the following parameters:

ParameterDescription
access_tokenA token you can use to call LiveChat APIs on behalf of the user.
account_idThe ID of the Agent's account
expires_inA number in seconds specifying how long the access_token will be valid; 28800 sec by default. When it expires, you will need to generate new access_token using refresh_token (read Using the refresh token for more details).
organization_idThe ID of the organization's account.
refresh_tokenA token that can be used to generate new access tokens.
scopeA comma-separated list of permissions an access_token has.
token_typeValue: Bearer

πŸ’‘ Got stuck? See common Errors.

EXCHANGE CODE FOR TOKEN
Copied!
curl "https://accounts.livechat.com/v2/token" \
  -X POST \
  -d "grant_type=authorization_code&\
  code=dal:test_tnlRmy73mg9eaFESA&\
  client_id=9cbf3a968289727cb3cdfe83ab1d9836&\
  client_secret=test_d7MEp1YYo3&\
  redirect_uri=https://my-application.com"
Response
Copied!
{
  "access_token": "dal:test_YTJQ6GDVgQf8kQDPw",
  "account_id": "b7eff798-f8df-4364-8059-649c35c9ed0c",
  "expires_in": 28800,
  "organization_id": "390e44e6-f1e6-0368c-z6ddb-74g14508c2ex",
  "refresh_token": "dal:test_gfalskcakg2347o8326",
  "scope": "chats--all:ro,chats--all:rw",
  "token_type": "Bearer"
}

Step 5: Use the token in API calls

Once you extract the token from the URL, your app can use it to sign requests to the LiveChat API. Your application should store the access_token in localStorage or a cookie until it expires. Caching the token prevents you from redirecting the user to LiveChat OAuth Server every time they visit your app.

Using the refresh token

When an access_token expires, your app needs to acquire a new one. To do that, it has to send an HTTP POST request using the refresh_token.

https://accounts.livechat.com/v2/token
Request
ParameterRequiredRequired with PKCE server side appRequired with PKCE javascript appDescription
grant_typeyesyesyesValue: refresh_token
refresh_tokenyesyesyesThe refresh token returned from when exchanging the code.
client_idyesyesyesClient Id from Developer Console (Authorization block)
client_secretyesyesnoUnique, confidential identifier of your app from Developer Console (Authorization block).
Response

The response is a JSON with the following parameters:

ParameterDescription
access_tokenA token you can use to call LiveChat APIs on behalf of the user.
account_idThe ID of the Agent's account
expires_inA number in seconds specifying how long the access_token will be valid. When it expires, you will need to generate new access_token using refresh_token (read Using the refresh token for more details).
organization_idthe ID of the organization's account.
scopeA comma-separated list of permissions an access_token has.
refresh_tokenA token that can be used to generate new access tokens.
token_typeValue: Bearer

πŸ’‘ Got stuck? See common Errors.

GET A NEW ACCESS TOKEN
Copied!
curl "https://accounts.livechat.com/v2/token" \
  -X POST \
  -d "grant_type=refresh_token&\
  refresh_token=dal:test_gfalskcakg2347o8326&\
  client_id=9cbf3a968289727cb3cdfe83ab1d9836&\
  client_secret=test_d7MEp1YYo3"
response
Copied!
{
  "access_token": "dal:test_YTJQ6GDVgQf8kQDPw",
  "account_id": "b7eff798-f8df-4364-8059-649c35c9ed0c",
  "expires_in": 28800,
  "organization_id": "390e44e6-f1e6-0368c-z6ddb-74g14508c2ex",
  "scope": "chats--all:ro,chats--all:rw",
  "refresh_token": "dal:test_gfalskcakg2347o8326",
  "token_type": "Bearer"
}

Revoking tokens

In some cases, a user may wish to revoke the access (the token) given to your application. The token can be either an access token or a refresh token. If it's an access token with a corresponding refresh token, both tokens will be revoked. To revoke a token, make a DELETE HTTP request to the following URL:

https://accounts.livechat.com/v2/token

Code snippets present two alternative ways of making the same request.

Request
ParameterRequiredDescription
codeyesThe value of the access_token or the refresh_token to revoke

πŸ’‘ Got stuck? See common Errors.

REVOKE A TOKEN
Copied!
curl "https://accounts.livechat.com/v2/token"
  -H "Authorization: Bearer <access_token|refresh token>"
  -X DELETE
response (200 OK)
Copied!
{}

Validating the access token

You can validate an access_token by making a GET HTTP request to the following URL:

https://accounts.livechat.com/v2/info

Please note that refresh tokens are not supported for direct validation. If an access token was obtained using a refresh token, the response will return both tokens.

Response

The response is a JSON with the following parameters:

ParameterDescription
access_tokenA token you can use to call LiveChat APIs on behalf of the user.
account_idThe ID of the Agent's account
client_idClient Id of your app
expires_inA number in seconds specifying how long the access_token will be valid. When it expires, you will need to generate new access_token using refresh_token (read Using the refresh token for more details).
organization_idThe ID of the organization's account.
scopeA comma-separated list of permissions an access_token has.
refresh_tokenA token that can be used to generate new access tokens. Returned optionally.
token_typeValue: Bearer

πŸ’‘ Got stuck? See common Errors.

VALIDATE AN ACCESS TOKEN
Copied!
curl "https://accounts.livechat.com/v2/info"
  -H "Authorization: Bearer <access_token>"
response
Copied!
{
    "access_token": "dal:test_YTJQ6GDVgQf8kQDPw",
    "account_id": "bbe8b147-d60e-46ac-a1e5-1e94b11ea6e1",
    "client_id": "9cbf3a968289727cb3cdfe83ab1d9836",
    "expires_in": 28725,
    "organization_id": "390e44e6-f1e6-0368c-z6ddb-74g14508c2ex",
    "scope": "chats--all:ro,chats--all:rw",
    "token_type": "Bearer"
}

Errors

If you get stuck or any error appears, please go to common Errors.

...

Join the community
Get in direct contact with us through Discord.
Follow us
Follow our insightful tweets and interact with out content.
Contribute
See something that's wrong or unclear? Submit a pull request.
Contact us
Want to share feedback? Reach us at: developers@livechat.com