Using an Authenticated Gateway in AI Gateway adds security by requiring a valid authorization token for each request. This feature is especially useful when storing logs, as it prevents unauthorized access and protects against invalid requests that can inflate log storage usage and make it harder to find the data you need. With Authenticated Gateway enabled, only requests with the correct token are processed.
Setting up Authenticated Gateway using the Dashboard
Go to the Settings for the specific gateway you want to enable authentication for.
Select Create authentication token to generate a custom token with the required Run permissions. Be sure to securely save this token, as it will not be displayed again.
Include the cf-aig-authorization header with your API token in each request for this gateway.
Return to the settings page and toggle on Authenticated Gateway.
Example requests with OpenAI
Using the OpenAI SDK:
Example requests with the Vercel AI SDK
Expected behavior
The following table outlines gateway behavior based on the authentication settings and header status:
Authentication Setting
Header Info
Gateway State
Response
On
Header present
Authenticated gateway
Request succeeds
On
No header
Error
Request fails due to missing authorization
Off
Header present
Unauthenticated gateway
Request succeeds
Off
No header
Unauthenticated gateway
Request succeeds
Was this helpful?
What did you like?
What went wrong?
Thank you for helping improve Cloudflare's documentation!