Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Environment:Cohere ai Cohere python Cohere API Credentials

From Leeroopedia
Knowledge Sources
Domains Infrastructure, Authentication
Last Updated 2026-02-15 14:00 GMT

Overview

API key credential configuration required for authenticating with the Cohere API via environment variables or constructor parameters.

Description

The Cohere Python SDK requires a valid API key to authenticate all requests. The SDK resolves credentials through a priority chain: constructor parameter first, then the `CO_API_KEY` environment variable, then the `COHERE_API_KEY` fallback. An optional `CO_API_URL` variable allows overriding the default production endpoint for private deployments or testing.

Usage

Use this environment for all Cohere API workflows. Every request made through `Client`, `AsyncClient`, `ClientV2`, or `AsyncClientV2` requires an API key. The key can be a static string or a callable that returns a string (for dynamic token refresh).

System Requirements

Category Requirement Notes
API Key Valid Cohere API key Obtainable from https://dashboard.cohere.com/api-keys
Network HTTPS access to api.cohere.com (port 443) Or custom endpoint via CO_API_URL

Dependencies

No additional packages beyond the base SDK.

Credentials

The following environment variables are used:

  • `CO_API_KEY`: Primary API key. Documented and preferred method.
  • `COHERE_API_KEY`: Fallback API key. Accepted but not documented in official docs.
  • `CO_API_URL`: Custom API endpoint URL. Overrides the default `https://api.cohere.com`.

Quick Install

# Set API key (preferred method)
export CO_API_KEY="your-api-key-here"

# Optional: override API endpoint for private deployments
export CO_API_URL="https://your-private-endpoint.example.com"

# Verify with Python
python -c "import cohere; c = cohere.ClientV2(); print('OK')"

Code Evidence

Dual environment variable fallback from `client.py:514-519`:

def _get_api_key_from_environment() -> typing.Optional[str]:
    """
    Retrieves the Cohere API key from specific environment variables.
    CO_API_KEY is preferred (and documented) COHERE_API_KEY is accepted (but not documented).
    """
    return os.getenv("CO_API_KEY", os.getenv("COHERE_API_KEY"))

Constructor parameter priority from `client.py:134-147`:

class Client(BaseCohere, CacheMixin):
    def __init__(
        self,
        api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None,
        *,
        base_url: typing.Optional[str] = os.getenv("CO_API_URL"),
        environment: ClientEnvironment = ClientEnvironment.PRODUCTION,
        ...
    ):
        if api_key is None:
            api_key = _get_api_key_from_environment()

URL normalization for Cohere domains from `client.py:123-128`:

def fix_base_url(base_url: typing.Optional[str]) -> typing.Optional[str]:
    if base_url is not None:
        if "cohere.com" in base_url or "cohere.ai" in base_url:
            return base_url.replace("/v1", "")
        return base_url
    return None

Common Errors

Error Message Cause Solution
`UnauthorizedError` (HTTP 401) Invalid or expired API key Verify key at https://dashboard.cohere.com/api-keys
`InvalidTokenError` (HTTP 498) Malformed or revoked token Generate a new API key from the dashboard
Client works but no key warning `CO_API_KEY` not set, key is `None` Set `CO_API_KEY` or pass `api_key=` to constructor

Compatibility Notes

  • Callable API Key: The `api_key` parameter accepts `typing.Callable[[], str]` for dynamic token refresh (e.g., from a secrets manager).
  • URL Stripping: If `CO_API_URL` contains `cohere.com` or `cohere.ai`, the SDK automatically strips `/v1` suffixes to avoid double-versioning.
  • V1 vs V2 Clients: Both `Client` (V1) and `ClientV2` read the same environment variables with the same priority.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment