Authentication
The Cloudflare TypeScript SDK supports multiple authentication methods to access the Cloudflare API. Choose the method that best fits your use case.API token (recommended)
API tokens are the preferred and most secure way to authenticate with the Cloudflare API. Tokens can be scoped to specific permissions and resources.Creating an API token
Access the Cloudflare Dashboard
Visit the Cloudflare Dashboard and navigate to My Profile > API Tokens.
Create a new token
Click Create Token and either:
- Select a pre-configured template (recommended for common use cases)
- Create a custom token with specific permissions
Configure permissions
Select the permissions and resources your token needs access to. Follow the principle of least privilege.
Using an API token
Set the token using theapiToken option or the CLOUDFLARE_API_TOKEN environment variable:
API key + email (legacy)
The API key and email combination is the previous authorization scheme. When possible, use API tokens instead for better security and granular permissions.Finding your API key
Using API key + email
Provide bothapiKey and apiEmail options, or use environment variables:
User service key
User service keys are used for specific API endpoints, such as the Origin CA certificates API. This is a specialized authentication method for certificate management.Finding your user service key
You can view or change your Origin CA key at: https://developers.cloudflare.com/fundamentals/api/get-started/ca-keys/#viewchange-your-origin-ca-keysUsing a user service key
Provide theuserServiceKey option or use the CLOUDFLARE_API_USER_SERVICE_KEY environment variable:
Authentication priority
When multiple authentication methods are configured, the SDK uses them in this order:- API key + email: If both
apiKeyandapiEmailare provided - API token: If
apiTokenis provided - User service key: If
userServiceKeyis provided
- API token:
Authorization: Bearer <token> - API key + email:
X-Auth-Key: <key>andX-Auth-Email: <email> - User service key:
X-Auth-User-Service-Key: <key>
Environment variables reference
All supported environment variables for authentication:| Environment Variable | Description | Auth Method |
|---|---|---|
CLOUDFLARE_API_TOKEN | Your API token | API Token (recommended) |
CLOUDFLARE_API_KEY | Your Global API Key | API Key + Email (legacy) |
CLOUDFLARE_EMAIL | Your Cloudflare account email | API Key + Email (legacy) |
CLOUDFLARE_API_USER_SERVICE_KEY | Your user service key | User Service Key |
Security best practices
Use API tokens instead of API keys
API tokens can be scoped to specific permissions and are more secure than Global API Keys.
Never commit credentials to version control
Always use environment variables or a secrets management system. Add
.env to your .gitignore.Rotate credentials regularly
Periodically rotate your API tokens and keys to minimize security risks.
Use scoped permissions
When creating API tokens, only grant the minimum permissions required for your use case.
Common errors
Authentication required
If you see this error, ensure you’ve provided valid credentials:Invalid credentials
If you receive a401 AuthenticationError, your credentials may be incorrect or expired:
Next steps
Quick start
Make your first authenticated API call
API Reference
Explore available API endpoints