AnyAPI uses API keys to authenticate requests. Your API keys carry many privileges, so be sure to keep them secure! Do not share your secret API keys in publicly accessible areas such as GitHub, client-side code, and so forth.
All API requests must be made over HTTPS. Calls made over plain HTTP will fail. API requests without authentication will also fail.

API Keys

Authentication to the API is performed via HTTP Bearer authentication. Provide your API key as a bearer token in the Authorization header.

Getting Your API Key

You can find and manage your API keys in the AnyAPI.ai Dashboard. Keep your API keys secure and rotate them regularly for optimal security.

Bearer Token Format

Include your API key in the Authorization header using the Bearer authentication scheme:
Authorization: Bearer YOUR_API_KEY

Basic Authentication Example

curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '
{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "Hello, world!"
        }
    ]
}'

Advanced cURL Examples

Basic Chat Completion

curl -X POST https://api.anyapi.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Explain quantum computing in simple terms."}
    ],
    "max_tokens": 150,
    "temperature": 0.7
  }'

Streaming Response

curl -X POST https://api.anyapi.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [
      {"role": "user", "content": "Write a short story about AI"}
    ],
    "stream": true
  }' \
  --no-buffer

With Custom Headers (Optional)

curl -X POST https://api.anyapi.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: unique-request-id-123" \
  -H "HTTP-Referer: https://yourapp.com" \
  -H "X-Title: My AI Application" \
  -d '{
    "model": "claude-3-sonnet",
    "messages": [
      {"role": "user", "content": "Hello from my application!"}
    ]
  }'

Error Handling

If your API key is missing or invalid, you’ll receive an error response:
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]}'
curl -X POST https://api.anyapi.ai/v1/chat/completions \
-H "Authorization: Bearer invalid_key_here" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]}'

Security Best Practices

Never expose your API keys in client-side code. API keys should only be used in server-side applications where they can be kept secure.

Environment Variables

Store your API key in environment variables:
ANYAPI_API_KEY=your_api_key_here

Rate Limiting

Your API key has rate limits associated with it. Include proper error handling for rate limit responses:
curl -X POST https://api.anyapi.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]}' \
  -w "HTTP Status: %{http_code}\n"

Testing Your Authentication

Use this simple curl command to test if your API key is working:
curl -X GET https://api.anyapi.ai/v1/models \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json"
A successful response will return a list of available models:
{
  "object": "list",
  "data": [
    {
      "id": "gpt-4",
      "object": "model",
      "created": 1687882411,
      "owned_by": "openai"
    },
    {
      "id": "gpt-3.5-turbo",
      "object": "model", 
      "created": 1677610602,
      "owned_by": "openai"
    }
    // ... more models
  ]
}

Next Steps