Skip to main content
This tutorial covers how to authenticate with the Gloo AI API. Authentication is required for all API endpoints and is done with a single API key passed in the Authorization header.

Overview

The Gloo AI API uses API key authentication. This process involves:
  1. Get an API Key - Obtain your API key from the Gloo AI Studio
  2. Set Your API Key - Store it securely in an environment variable
  3. Use Your API Key in API Calls - Include it in the Authorization header of every request

Prerequisites

Before starting, ensure you have:

Step 1: Environment Setup

First, set up your environment variable to securely store your API key:

Environment Variables

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

Step 2: Using Your API Key in API Calls

Include your API key in the Authorization header of every API request:

Example API Request

The examples below call Completions V2 (/ai/v2/chat/completions). The same API key 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 your API key in source code
  • Use environment variables or secure credential storage
  • Add .env files to your .gitignore

2. Network Security

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

3. Error Handling

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

Common Issues and Solutions

Issue: 401 Unauthorized

Cause: Invalid or missing API key Solution: Verify your GLOO_API_KEY is set correctly and has not been revoked

Issue: 403 Forbidden

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

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

Deprecated: OAuth2 Client Credentials

The OAuth2 client credentials flow described below is deprecated. New integrations should use the API key authentication shown above. This section is preserved only for existing integrations that have not yet migrated.
If you are maintaining an existing integration that still uses OAuth2 client credentials, you can exchange your Client ID and Client Secret for a temporary access token using the token endpoint.

Environment Variables

Token Exchange

Token Management

Access tokens obtained through the OAuth2 flow expire after one hour. Implement token management to handle expiration in legacy integrations:
The authentication patterns shown here work across all Gloo AI API endpoints, providing a secure foundation for your applications.