---
updatedAt: 2025-05-08T22:50:36.000Z
agentTools:
  projectIndex: https://docs-api.immersity.ai/llms.txt
---

# Get Access Token

```node Node
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';

/**
 * Reading configuration from environment
 */

const CLIENT_ID = process.env.BACKEND_CLIENT_ID;
const CLIENT_SECRET = process.env.BACKEND_CLIENT_SECRET;

if (!CLIENT_ID || !CLIENT_SECRET) {
  console.error('Error. In order to authenticate against Immersity AI' +
    ' API, you need to provide BACKEND_CLIENT_ID and BACKEND_CLIENT_SECRET ' +
    'env vars');
  process.exit(1);
}

/**
 * Needs to be configured before running the script;
 */
const IMMERSITYAI_LOGIN_OPENID_TOKEN_URL = 'https://auth.immersity.ai/auth/realms/immersity/protocol/openid-connect/token';

export default function getAccessToken() {
  try {
    console.log('Acquiring access token from Immersity Login...');
    const tokenResponse = await axios.post(IMMERSITYAI_LOGIN_OPENID_TOKEN_URL, {
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      grant_type: 'client_credentials',
    }, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    });

    const accessToken = tokenResponse.data.access_token;
    console.log(`\nImmersity AI Login AccessToken acquired: ${accessToken}`);

    return accessToken;
    // We omit the error handling in this example for simplicity, but
    // you should always check for a returned status & errors from the API
    // in real code.
  } catch (e: any) {
    if (e.hasOwnProperty('message') || e.hasOwnProperty('response')) {
      console.error(`Error. Unhandled exception: ${JSON.stringify(e.message)}`);
      console.error(`Error body: ${JSON.stringify(e.response?.data)}`);
    } else {
      console.error(`Error. Unhandled exception: ${JSON.stringify(e)}`);
    }
  }
}


```

```python Python
import sys
import os
import uuid
import requests

# print("\n".join(sys.path))
# Reading configuration from environment
if sys.prefix == sys.base_prefix:
    print('Error. You should run application in virtualenv to not pollute global dependencies. Please refer to '
          'README.md for instructions on how to set it up', file=sys.stderr)
    print(f'sys.prefix: {sys.prefix}')
    print(f'sys.base_prefix: {sys.base_prefix}')
    exit(1)

# Get the environment variables
CLIENT_ID = os.getenv('BACKEND_CLIENT_ID')
CLIENT_SECRET = os.getenv('BACKEND_CLIENT_SECRET')

# Check that they are all set
if not CLIENT_ID or not CLIENT_SECRET:
    print('Error. In order to authenticate against Immersity API, you need to provide BACKEND_CLIENT_ID and '
          'BACKEND_CLIENT_SECRET env vars', file=sys.stderr)
    exit(1)

IMMERSITYAI_LOGIN_OPENID_TOKEN_URL = 'https://auth.immersity.ai/auth/realms/immersity/protocol/openid-connect/token'

def getAccessToken():
    print('Acquiring access token from Immersity AI Login...')
    token_response = requests.post(IMMERSITYAI_LOGIN_OPENID_TOKEN_URL, data={
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'grant_type': 'client_credentials'
    }).json()
    access_token = token_response['access_token']
    print(f'\nImmersity AI Login AccessToken acquired: {access_token}')
    
    return access_token
    
		# We omit the error handling in this example for simplicity, but
    # you should always check for a returned status & errors from the API
    # in real code.

except Exception as e:
    print('Error. Unhandled exception: ' + str(e), file=sys.stderr)

```

# Provide Client Credentials

<!-- node@8-16 -->
<!-- python@15-23 -->

Inject your client credentials via environment variables. It's against best practice to hard-code them in your program.

You can generate ClientID and Client Secret in Immersity AI Login API Section.

# Call the Token Endpoint

<!-- node@26-36 -->
<!-- python@29-35 -->

Send a POST request to the Token endpoint, providing all the described parameters and headers.