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
1
Access the Cloudflare Dashboard
Visit the Cloudflare Dashboard and navigate to My Profile > API Tokens.
2
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
3
Configure permissions
Select the permissions and resources your token needs access to. Follow the principle of least privilege.
4
Save your token
Copy the generated token immediately. You won’t be able to view it again.
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
1
Access your profile
Go to My Profile > API Tokens in the Cloudflare Dashboard.
2
View Global API Key
Scroll to the API Keys section and click View next to Global API Key.
3
Copy the key
Enter your password to reveal and copy your Global 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:Security best practices
1
Use API tokens instead of API keys
API tokens can be scoped to specific permissions and are more secure than Global API Keys.
2
Never commit credentials to version control
Always use environment variables or a secrets management system. Add
.env to your .gitignore.3
Rotate credentials regularly
Periodically rotate your API tokens and keys to minimize security risks.
4
Use scoped permissions
When creating API tokens, only grant the minimum permissions required for your use case.
5
Monitor token usage
Regularly review token usage in the Cloudflare Dashboard and revoke unused tokens.
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