Genesys Cloud - Developer Community!

 View Only

Sign Up

  • 1.  Authentication for API using JavaScript

    Posted 06-24-2025 11:32

    Hello Community,

    I am seeking some guidance or help on this problem. I am an intern working on an API project. To begin I am tasked with getting an API running from the API explorer on VS Code. I keep running into the problem with authorization using the Bearer token. I have got the API to run using cURL. However, I want it to run on JS. When I put in my Bearer token I am getting an error message stating:

    "message: 'No authentication bearer token specified in authorization header.',

      code: 'authentication.required',
      status: 401,
      contextId: '9e326059-38fc-43f1-8f0a-7c92ca91ef46',
      details: [],
      errors: []"
    I used cURL to create a Bearer token and used the token in JS but it still gives the same error message.
    Then I changed the code instead using the Bearer token to Credit Credentials Grant login type. Still says the same error message.


    Questions:

    • Can you run a Genesys API from VS Code?
    • Is there a language that will allow me to edit an API?

    Below I have posted the API's code I am trying to run. (On line 7, I left the 'access token' part blank for security reasons 'client.setAccessToken("your_access_token" );

    const platformClient = require("purecloud-platform-client-v2");

    const client = platformClient.ApiClient.instance;
    client.setEnvironment(platformClient.PureCloudRegionHosts.us_east_1); // Genesys Cloud region

    // Manually set auth token or use loginImplicitGrant(...) or loginClientCredentialsGrant(...) or loginPKCEGrant(...)
    client.setAccessToken("your_access_token");

    let apiInstance = new platformClient.GroupsApi();

    let opts = {
      "pageSize": 25, // Number | Page size
      "pageNumber": 1, // Number | Page number
      "id": ["id_example"], // [String] | id
      "jabberId": ["jabberId_example"], // [String] | A list of jabberIds to fetch by bulk (cannot be used with the id parameter)
      "sortOrder": "ASC" // String | Ascending or descending sort order
    };

    // Get a group list
    apiInstance.getGroups(opts)
      .then((data) => {
        console.log(`getGroups success! data: ${JSON.stringify(data, null, 2)}`);
      })
      .catch((err) => {
        console.log("There was a failure calling getGroups");
        console.error(err);
      });

    I have the clientId and clientSecret, I changed the line of code

    'client.setAccessToken("your_access_token");  to  

    client.loginClientCredentialsGrant("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
      .then(() => {
        const apiInstance = new platformClient.GroupsApi();

    I still get the error message.

    Our end goal is to hopefully find or create an API that runs reports on a schedule or an API that can create reports of what the department is asking for. 

    Example:

    Help Desk wants an API that can report how many calls came in for the week or month, which calls either failed, disconnected, did not answer etc.


    #PlatformAPI

    ------------------------------
    Alberto Hernandez
    ------------------------------


  • 2.  RE: Authentication for API using JavaScript

    Posted 06-24-2025 12:01

    Hello,

    When you use client.setAccessToken("your_access_token"); , the your_access_token must only contains the token.

    I mean that if you used/copied something like "bearer XYZ", remove "bearer " and only use "XYZ".

    Regards,



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------



  • 3.  RE: Authentication for API using JavaScript

    Posted 06-24-2025 14:44

    Hello,

    I tried that out and I still am getting this error:

    There was a failure calling getGroups
    {
      message: 'No authentication bearer token specified in authorization header.',
      code: 'authentication.required',
      status: 401,
      contextId: 'cf307017-482c-47e0-98a5-530e47fae059',
      details: [],
      errors: []
    }
    Logged in and token is set!


    ------------------------------
    Alberto Hernandez
    ------------------------------



  • 4.  RE: Authentication for API using JavaScript

    Posted 06-24-2025 15:13
    Edited by John Carnell 06-26-2025 15:12

    Hello,

    I ended up creating a new bearer token through cURL and it finally ran. Below is the code I used:

    const platformClient = require("purecloud-platform-client-v2");

    const client = platformClient.ApiClient.instance;
    client.setEnvironment(platformClient.PureCloudRegionHosts.us_east_1); // Genesys Cloud region

    // Manually set auth token or use loginImplicitGrant(...) or loginClientCredentialsGrant(...) or loginPKCEGrant(...)
    client.setAccessToken("REMOVED");

    let apiInstance = new platformClient.GroupsApi();

    let opts = {
      "pageSize": 25, // Number | Page size
      "pageNumber": 1, // Number | Page number
      "id": ["id_example"], // [String] | id
      "jabberId": ["jabberId_example"], // [String] | A list of jabberIds to fetch by bulk (cannot be used with the id parameter)
      "sortOrder": "ASC" // String | Ascending or descending sort order
    };

    // Get a group list
    apiInstance.getGroups(opts)
      .then((data) => {
        console.log(`getGroups success! data: ${JSON.stringify(data, null, 2)}`);
      })
      .catch((err) => {
        console.log("There was a failure calling getGroups");
        console.error(err);
      });
    Although it ran, I am not seeing the data I need or that would be presented throught the API. This is the output I am getting:

    getGroups success! data: {
      "entities": [],
      "pageSize": 0,
      "pageNumber": 1,
      "total": 0,
      "lastUri": "/api/v2/groups?pageSize=0&pageNumber=1",
      "firstUri": "/api/v2/groups?pageSize=0&pageNumber=1",
      "selfUri": "/api/v2/groups?pageSize=0&pageNumber=1",
      "pageCount": 0
    }

    Thank you!



    ------------------------------
    Alberto Hernandez
    ------------------------------



  • 5.  RE: Authentication for API using JavaScript

    Posted 07-01-2025 06:05

    Hello,

    "Although it ran, I am not seeing the data I need or that would be presented throught the API"

    The optional parameter values you are using in your code for the GET /api/v2/groups endpoint are incorrect:

    "let opts = {
      "pageSize": 25, // Number | Page size
      "pageNumber": 1, // Number | Page number
      "id": ["id_example"], // [String] | id
      "jabberId": ["jabberId_example"], // [String] | A list of jabberIds to fetch by bulk (cannot be used with the id parameter)
      "sortOrder": "ASC" // String | Ascending or descending sort order
    };"

    You need to change them or remove them - I am referring to the jabberId and the id arrays. If you want to retrieve a group (or set of groups) based on its id (based on their ids), you need to set it in the "id" optional parameter. Otherwise, just remove this optional parameter (and jabberId as well).

    Regards,



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------