Skip to main content
This tutorial covers how to authenticate with the Gloo AI API using OAuth2 client credentials flow. Authentication is required for all API endpoints and involves exchanging your Client ID and Client Secret for a temporary access token.

Overview

The Gloo AI API uses OAuth2 client credentials flow for authentication. This process involves:
  1. Get Client Credentials - Obtain your Client ID and Client Secret from the Gloo AI Studio
  2. Exchange for Access Token - Use your credentials to get a temporary bearer token
  3. Use Token in API Calls - Include the bearer token in all API requests
  4. Handle Token Expiration - Refresh tokens when they expire

Prerequisites

Before starting, ensure you have:

Step 1: Environment Setup

First, set up your environment variables to securely store your credentials:

Environment Variables

Create a .env file in your project root:
For Go and Java, you can also export them directly:

Step 2: Token Exchange

Exchange your Client ID and Client Secret for an access token by calling the OAuth2 token endpoint:

Step 3: Token Management

Access tokens are temporary and expire after a certain period. Implement token management to handle expiration:

Step 4: Using Tokens in API Calls

Once you have a valid access token, include it in the Authorization header of your API requests:

Example API Request

The examples below call Completions V2 (/ai/v2/chat/completions). The same bearer token works on the Responses API (v1) (/ai/v1/responses), Gloo’s recommended endpoint for new integrations — only the URL and request shape differ.

Security Best Practices

1. Environment Variables

  • Never hardcode credentials in your source code
  • Use environment variables or secure credential storage
  • Add .env files to your .gitignore

2. Token Storage

  • Store tokens securely in memory
  • Don’t persist tokens to disk in production
  • Implement proper token rotation

3. Network Security

  • Always use HTTPS for API calls
  • Implement proper error handling
  • Use secure HTTP client configurations

4. Error Handling

  • Handle authentication failures gracefully
  • Implement retry logic for transient failures
  • Log authentication events securely

Common Issues and Solutions

Issue: 401 Unauthorized

Cause: Token expired or invalid credentials Solution: Implement token refresh logic and verify credentials

Issue: 403 Forbidden

Cause: Insufficient permissions Solution: Check your API access levels in the Studio

Issue: Token Expired

Cause: Access token has exceeded its lifetime Solution: Implement automatic token refresh before expiration

Testing Your Implementation

Create a simple test to verify your authentication setup:

Working Code Sample

View Complete Code

Clone or browse the complete working examples for all 6 languages (JavaScript, TypeScript, Python, PHP, Go, Java) with setup instructions.

Next Steps

Now that you have authentication set up, you can use it in other Gloo AI tutorials:
  1. Responses API - Build on Gloo’s recommended API surface (text, vision, image generation, tool use)
  2. Building Interactive Chat - Create conversational experiences
  3. Using the Completions API - Generate text completions (includes intelligent routing, model_family, and grounded completions)
  4. API Reference - Explore all available endpoints
The authentication patterns shown here work across all Gloo AI API endpoints, providing a secure foundation for your applications.