Authentication overview

Cohere's API utilizes API keys as the primary method for authenticating requests. This approach provides a straightforward mechanism for developers to secure access to Cohere's suite of large language models and other AI capabilities, including text generation, embeddings, and reranking services. Each API key is associated with a Cohere account and grants permissions based on the account's subscription level and access configurations. All API communication with Cohere is secured via HTTPS (TLS), ensuring that data transmitted between client applications and Cohere's servers is encrypted and protected from interception.

When making an API request, the API key must be included in the request headers, typically as a bearer token in the Authorization header. This standard practice aligns with common API security patterns and allows Cohere to verify the identity of the requesting application before processing the request. Proper management and safeguarding of these API keys are crucial for maintaining the security and integrity of applications built on Cohere's platform.

Supported authentication methods

Cohere primarily supports API key authentication for programmatic access. This method is suitable for most integration scenarios, from development environments to production deployments. The table below outlines the key characteristics of this authentication method:

Method When to Use Security Level
API Key (Bearer Token)
  • Server-side applications
  • Backend services
  • Development and testing environments
  • Automated scripts and integrations
High (when managed securely)

While API keys are the standard, the underlying security relies on the secure transmission protocol HTTPS. The IETF RFC 2818 specifies HTTPS as a secure HTTP variant, which encrypts communication between the client and server. This ensures that the API key itself, along with any data sent or received, is protected during transit over the internet.

Getting your credentials

To obtain your Cohere API key, you need to create an account and generate a key through the Cohere dashboard. The process typically involves these steps:

  1. Sign Up/Log In: Navigate to the Cohere platform and either sign up for a new account or log in to an existing one.
  2. Access API Keys Section: Once logged in, locate the section related to API keys or developer settings within your dashboard. This is often found under account settings or a dedicated 'API Keys' tab.
  3. Generate New Key: Follow the prompts to generate a new API key. Some platforms allow you to name your key for easier identification, especially if you plan to use multiple keys for different projects or environments.
  4. Copy and Store Securely: After generation, the API key will be displayed. It is crucial to copy this key immediately and store it in a secure location. For security reasons, Cohere typically only displays the full key once, and you may not be able to retrieve it again from the dashboard if lost.

For detailed, step-by-step instructions, refer to the official Cohere getting started guide.

Authenticated request example

Once you have your API key, you can use it to make authenticated requests to the Cohere API. The key is typically passed in the Authorization header with the Bearer scheme. Here's an example using Python, one of the Cohere supported SDKs:


import cohere
import os

# It's best practice to load your API key from an environment variable
COHERE_API_KEY = os.getenv("COHERE_API_KEY")

if not COHERE_API_KEY:
    raise ValueError("COHERE_API_KEY environment variable not set.")

co = cohere.Client(COHERE_API_KEY)

response = co.generate(
    model='command-r',
    prompt='Write a short poem about the ocean.',
    max_tokens=50,
    temperature=0.9,
    k=0,
    stop_sequences=[],
    return_likelihoods='NONE'
)

print(response.generations[0].text)

In this Python example:

  • cohere.Client(COHERE_API_KEY) initializes the Cohere client with your API key.
  • The API key is retrieved from an environment variable (COHERE_API_KEY), which is a recommended security practice to avoid hardcoding credentials directly into your codebase.
  • The co.generate() method then makes an authenticated call to the Cohere API to generate text based on the provided prompt and parameters.

Similar patterns apply to other Cohere SDKs like JavaScript, Go, and Java, where the API key is passed during client initialization or explicitly in the request headers.

Security best practices

Securing your Cohere API keys is paramount to prevent unauthorized access and potential misuse of your account. Adhere to these best practices:

  • Do Not Hardcode API Keys: Never embed your API keys directly into your source code. Instead, use environment variables, configuration files, or secret management services to store and retrieve them. This prevents keys from being exposed in version control systems or publicly accessible repositories.
  • Use Environment Variables: For server-side applications, storing API keys as environment variables (e.g., COHERE_API_KEY=your_key_here) is a common and effective practice. Most programming languages and frameworks provide easy ways to access these variables.
  • Implement Secret Management: For more complex deployments or enterprise-level applications, consider using dedicated secret management solutions like AWS Secrets Manager, Google Cloud Secret Manager, or Azure Key Vault. These services provide secure storage, rotation, and access control for sensitive credentials.
  • Restrict Access to Keys: Limit who has access to your API keys. Only individuals or systems that absolutely require access should have it. Implement role-based access control (RBAC) where possible.
  • Regularly Rotate Keys: Periodically generate new API keys and revoke old ones. This practice, known as key rotation, reduces the window of opportunity for a compromised key to be exploited. The recommended frequency for rotation depends on your security policy and risk assessment.
  • Monitor API Usage: Regularly review your Cohere API usage logs for any unusual activity. Spikes in usage or requests from unexpected geographical locations could indicate a compromised key.
  • Secure Your Development Environment: Ensure that your local development machine and any CI/CD pipelines are secure. Protect against malware and unauthorized access to prevent key theft.
  • Avoid Client-Side Exposure: Never expose your Cohere API keys directly in client-side code (e.g., JavaScript in a web browser or mobile app). If your application requires client-side interaction with Cohere, route requests through a secure backend server that can manage and authenticate with the API key on behalf of the client.
  • Understand Rate Limits and Quotas: While not directly an authentication security measure, understanding Cohere's rate limits and quotas can help in identifying and mitigating potential abuse if a key is compromised.

Implementing these practices helps to ensure the confidentiality and integrity of your Cohere API interactions, aligning with general API security principles outlined by organizations like the OWASP API Security Project.