MCPcopy Index your code
hub / github.com/cohere-ai/cohere-typescript

github.com/cohere-ai/cohere-typescript @8.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 8.0.0 ↗ · + Follow
1,102 symbols 2,226 edges 826 files 73 documented · 7%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Cohere TypeScript SDK

npm shield fern shield

The Cohere Typescript SDK allows access to Cohere models across many different platforms: the cohere platform, AWS (Bedrock, Sagemaker), Azure, GCP and Oracle OCI. For a full list of support and snippets, please take a look at the SDK support docs page.

Documentation

Cohere documentation and API reference is available here.

Installation

npm i -s cohere-ai

Usage

import { CohereClientV2 } from "cohere-ai";

const cohere = new CohereClientV2({});

(async () => {
  const response = await cohere.chat({
    model: 'command-a-03-2025',
    messages: [
      {
        role: 'user',
        content: 'hello world!',
      },
    ],
  });

  console.log(response);
})();

Streaming

The SDK supports streaming endpoints. To take advantage of this feature for chat, use chatStream.

import { CohereClientV2 } from "cohere-ai";

const cohere = new CohereClientV2({});

(async () => {
  const stream = await cohere.chatStream({
    model: 'command-a-03-2025',
    messages: [
      {
        role: 'user',
        content: 'hello world!',
      },
    ],
  });

  for await (const chatEvent of stream) {
    if (chatEvent.type === 'content-delta') {
      console.log(chatEvent.delta?.message);
    }
  }
})();

Errors

When the API returns a non-success status code (4xx or 5xx response), a subclass of CohereError will be thrown:

import { CohereClientV2, CohereError, CohereTimeoutError } from "cohere-ai";

const cohere = new CohereClient({
    token: "YOUR_API_KEY",
});

(async () => {
    try {
        await cohere.generate(/* ... */);
    } catch (err) {
        if (err instanceof CohereTimeoutError) {
            console.log("Request timed out", err);
        } else if (err instanceof CohereError) {
            // catch all errors
            console.log(err.statusCode);
            console.log(err.message);
            console.log(err.body);
        }
    }
})();

AWS Support (Bedrock & SageMaker)

To use Cohere models on AWS Bedrock or SageMaker, import the AWS clients from the cohere-ai/aws subpath and install the required peer dependencies:

npm install @aws-sdk/credential-providers @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4
import { BedrockClient } from "cohere-ai/aws";

const cohere = new BedrockClient({
  awsRegion: "us-east-1",
});

const response = await cohere.chat({
  model: "cohere.command-a-03-2025",
  messages: [{ role: "user", content: "hello world!" }],
});

The following clients are available from cohere-ai/aws:

  • BedrockClient / BedrockClientV2 - AWS Bedrock
  • SagemakerClient / SagemakerClientV2 - AWS SageMaker
  • AwsClient / AwsClientV2 - Base AWS client

The AWS dependencies are optional peer dependencies and are only required if you use the AWS clients. They will not be installed automatically.

Beta status

This SDK is in beta, and while we will try to avoid it, there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version in your package.json file. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributing

While we value open-source contributions to this SDK, the code is generated programmatically. Additions made directly would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

Extension points exported contracts — how you extend this code

CrossPlatformFormData (Interface)
(no doc) [6 implementers]
src/core/form-data-utils/FormDataWrapper.ts
RequestHeadersStage (Interface)
(no doc) [3 implementers]
tests/mock-server/mockEndpointBuilder.ts
RequestOptions (Interface)
(no doc)
src/Client.ts
AwsDeps (Interface)
(no doc)
src/aws-utils.ts
BaseRequestOptions (Interface)
(no doc)
src/BaseClient.ts
Raw (Interface)
(no doc)
src/serialization/client/requests/EmbedRequest.ts
EmbedRequest (Interface)
(no doc)
src/api/client/requests/EmbedRequest.ts
CustomMatchers (Interface)
(no doc)
tests/setup.ts

Core symbols most depended-on inside this repo

jsonBody
called by 685
tests/mock-server/mockEndpointBuilder.ts
respondWith
called by 464
tests/mock-server/mockEndpointBuilder.ts
statusCode
called by 464
tests/mock-server/mockEndpointBuilder.ts
build
called by 464
tests/mock-server/mockEndpointBuilder.ts
mockEndpoint
called by 464
tests/mock-server/MockServer.ts
createServer
called by 464
tests/mock-server/MockServerPool.ts
get
called by 318
tests/mock-server/mockEndpointBuilder.ts
post
called by 252
tests/mock-server/mockEndpointBuilder.ts

Shape

Interface 541
Method 274
Function 183
Class 104

Languages

TypeScript100%

Modules by API surface

tests/mock-server/mockEndpointBuilder.ts58 symbols
src/Client.ts35 symbols
src/core/streaming-fetcher/streaming-utils.ts33 symbols
src/core/form-data-utils/FormDataWrapper.ts26 symbols
src/core/logging/logger.ts25 symbols
src/core/file/file.ts19 symbols
src/api/resources/finetuning/client/Client.ts18 symbols
src/core/stream/Stream.ts17 symbols
src/api/resources/connectors/client/Client.ts16 symbols
src/core/fetcher/Headers.ts14 symbols
src/api/resources/datasets/client/Client.ts14 symbols
src/core/fetcher/HttpResponsePromise.ts13 symbols

For agents

$ claude mcp add cohere-typescript \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page