Authentication Patterns
Common authentication patterns for generated SDKs — Bearer, API key, Basic, and custom auth.
OpenAPI specs use various authentication schemes. AutoSDK generates appropriate constructors and auth handling for each.
HTTP Bearer Token
The most common pattern for AI APIs:
autosdk generate spec.json --security-scheme Http:Header:BearerGenerated constructor:
public MyApiClient(string apiKey, HttpClient? httpClient = null)
{
// Sets Authorization: Bearer {apiKey}
}Usage:
using var client = new MyApiClient("sk-your-api-key");SDKs using this: OpenAI, Anthropic, Mistral, Together, ElevenLabs, Jina, VoyageAI
API Key in Header
For APIs that use a custom header:
autosdk generate spec.json --security-scheme ApiKey:Header:x-api-keyGenerated constructor:
public MyApiClient(string apiKey, HttpClient? httpClient = null)
{
// Sets x-api-key: {apiKey} header
}SDKs using this: Replicate, Leonardo
API Key in Query String
For APIs that pass the key as a query parameter:
autosdk generate spec.json --security-scheme ApiKey:Query:api_keySDKs using this: Roboflow
HTTP Basic Auth
For APIs using username/password:
autosdk generate spec.json --security-scheme Http:Header:BasicGenerated constructor:
public MyApiClient(string username, string password,
HttpClient? httpClient = null)
{
// Sets Authorization: Basic {base64(username:password)}
}SDKs using this: Langfuse (public key + secret key)
Custom Auth (Partial Hook)
When the generated auth doesn't match the provider's expectations, override with a partial hook:
// MyApiClient.Auth.cs
public partial class MyApiClient
{
partial void Authorized(HttpRequestMessage request)
{
// Deepgram uses "Token" prefix instead of "Bearer"
request.Headers.Authorization =
new AuthenticationHeaderValue("Token", _apiKey);
}
}SDKs using this: Deepgram (Token prefix), Mem0 (Token prefix)
Multiple Security Schemes
Some APIs support multiple auth methods:
autosdk generate spec.json \
--security-scheme Http:Header:Bearer \
--security-scheme ApiKey:Header:x-api-keySpec Patching for Missing Auth
Many specs don't properly define security schemes. Patch them in generate.sh:
# Add Bearer auth to a spec that's missing it
cat openapi.json | jq '
.components.securitySchemes = {
"BearerAuth": {
"type": "http",
"scheme": "bearer"
}
} |
.security = [{"BearerAuth": []}]
' > patched.json
autosdk generate patched.json --output GeneratedConverting apiKey to Bearer
Many specs define auth as apiKey type when it's actually Bearer:
cat openapi.json | jq '
.components.securitySchemes = (
.components.securitySchemes | to_entries | map(
if .value.type == "apiKey" then
.value = {"type": "http", "scheme": "bearer"}
else . end
) | from_entries
)
' > patched.jsonThis is the most common auth fix across tryAGI SDKs.
Last updated on