Authentication & API Keys

Complete guide to authenticating API requests with Twig AI.

Authentication Methods

Best for: Server-to-server, automated workflows, production applications

Format: Bearer token in Authorization header

curl -X GET https://api.twig.so/api/agents \
  -H "Authorization: Bearer sk_live_abc123..."

OAuth 2.0 (For User Context)

Best for: User-specific actions, third-party integrations

Flow:

  1. Redirect user to authorization URL

  2. User grants permission

  3. Receive authorization code

  4. Exchange for access token

  5. Use access token in requests

Generating API Keys

Via UI

  1. Navigate to SettingsAPI Keys

  2. Click Generate New API Key

  3. Provide details:

    • Name: "Production API", "Development", etc.

    • Scope: Permissions (see below)

    • Expiration: Optional expiry date

  4. Copy key immediately (shown only once)

  5. Store securely

Via API

API Key Types

Organization Keys

  • Access all resources in organization

  • Shared across team

  • Higher permissions

  • Require admin to create

Personal Keys

  • User-specific access

  • Limited to user's permissions

  • Can be self-generated

  • Tied to user account

Scoped Keys

  • Limited permissions

  • Specific operations only

  • Most secure

  • Recommended for production

API Key Scopes

Scope
Permissions

chat

Chat/completion requests

agents:read

List and view agents

agents:write

Create/edit agents

agents:delete

Delete agents

data:read

View data sources

data:write

Modify data sources

analytics:read

View analytics

admin

Full access

Example:

Using API Keys

Request Header

Examples

cURL:

JavaScript:

Python:

Key Management Best Practices

Security

Do:

  • Store keys in environment variables

  • Use different keys for dev/staging/prod

  • Rotate keys regularly (every 90 days)

  • Use scoped keys with minimum permissions

  • Revoke compromised keys immediately

Don't:

  • Hardcode keys in source code

  • Commit keys to version control

  • Share keys via email/chat

  • Use same key across all environments

  • Give keys broader scope than needed

Storage

Environment Variables:

Secret Managers:

  • AWS Secrets Manager

  • Google Cloud Secret Manager

  • HashiCorp Vault

  • Azure Key Vault

Configuration:

Rotation

Manual Rotation:

  1. Generate new key

  2. Update applications

  3. Test thoroughly

  4. Revoke old key

  5. Monitor for errors

Automated Rotation:

Error Handling

Invalid Key

Response:

Solutions:

  • Verify key is correct

  • Check for extra spaces

  • Ensure key hasn't been revoked

  • Verify key hasn't expired

Expired Key

Response:

Solution: Generate new key

Insufficient Permissions

Response:

Solution: Use key with appropriate scopes

Monitoring

Track Usage

View API key usage:

  • Request count

  • Success/failure rate

  • Last used timestamp

  • Geographic distribution

Set Alerts

Troubleshooting

Key Not Working

  1. Check format: Authorization: Bearer KEY

  2. Verify key status (not revoked)

  3. Check expiration date

  4. Verify scopes match operation

  5. Test with different endpoint

Intermittent Failures

  1. Check rate limits

  2. Verify network connectivity

  3. Review error logs

  4. Check key permissions

  5. Monitor API status page

Next Steps

Last updated