Skip to content

Invalid token

The following details provide a summary of the Invalid token error, explain the cause, and present recommended solutions.

What happens

When you send a request to a CalypsoAI endpoint with an invalid authorization token, the request fails.

The following fragment of a full error response shows you the key part.

python
calypsoai.clients.base.RequestError: 'GET https://www.us1.calypsoai.app/backend/v1/campaigns'
responded with status 401
Invalid token

Why it happens

The CalypsoAI API requires a valid authorization token to be provided in all requests to verify that the request is sent by an authorized user. If the authorization token is invalid, our API can't authenticate the request and responds with the Invalid token error.

The two most common causes for this error are the following:

  • An expired authorization token
    Authorization tokens have an expiration date. After the expiration date, the CalypsoAI API is no longer able to verify the user and authenticate the request.
  • An invalid environment URL value

    ENVIRONMENT URL

    For on-prem users, authorization tokens are only valid in the environment in which they are created.

    For SaaS users, there is only one environment, and all authorization tokens created in the SaaS environment are valid.

    • In an on-prem instance, the URL parameter might be missing.

      DEFAULT URL

      By default, the URL parameter in the SDK points to the SaaS environment. If you don't include the parameter in your request, the request automatically points to the SaaS environment.

    • In a SaaS instance, the URL of an on-prem instance might be present.
    • When you have multiple on-prem instances, your request might include the authorization token and the environment URL for different on-prem instances, not for the same one.

How to fix it

To resolve the issue, you must first identify the cause.

  1. Make sure your authorization token is not expired.

    TOKEN EXPIRATION

    To check the expiration status of your authorization tokens, in the CalypsoAI Platform, go to Configuration > API tokens.

  2. Make sure the environment URL in your request is present, is valid, and corresponds to your authorization token for the relevant environment (SaaS or on-prem).

    python
    from calypsoai import CalypsoAI
    
    # Define the URL and token for CalypsoAI
    CALYPSOAI_URL="VALID-URL"
    CALYPSOAI_TOKEN="VALID-TOKEN" # Make sure your entered authorization token corresponds to the environment URL in which you created it
    
    # Initialize the CalypsoAI client
    cai = CalypsoAI(url=CALYPSOAI_URL, token=CALYPSOAI_TOKEN)
    
    # Get all campaigns
    campaigns = [campaign.model_dump_json() for campaign in cai.campaigns.iterate()]
    print(campaigns)

Updated at: