For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
    • Overview
    • Usage for Agents
  • TypeScript SDK
    • Overview
      • Analytics
      • APIKeys
      • Chat
      • Credits
      • Embeddings
      • Endpoints
      • Generations
      • Guardrails
      • OAuth
      • Organization
      • Providers
      • Rerank
      • Beta.Responses
      • Speech
      • VideoGeneration
      • Workspaces
  • DevTools
    • Overview
    • Migrating to @openrouter/agent
LogoLogo
ModelsChatRankingsDocs
On this page
  • Overview
  • Available Operations
  • send
  • Example Usage: guardrail-blocked
  • Standalone function
  • Example Usage: insufficient-permissions
  • Standalone function
  • Parameters
  • Response
  • Errors
TypeScript SDKAPI Reference

Chat - TypeScript SDK

Chat method reference
Was this page helpful?
Previous

Credits - TypeScript SDK

Credits method reference
Next
Built with

The TypeScript SDK and docs are currently in beta. Report issues on GitHub.

Overview

Available Operations

  • send - Create a chat completion

send

Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes.

Example Usage: guardrail-blocked

1import { OpenRouter } from "@openrouter/sdk";
2
3const openRouter = new OpenRouter({
4 httpReferer: "<value>",
5 appTitle: "<value>",
6 appCategories: "<value>",
7 apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
8});
9
10async function run() {
11 const result = await openRouter.chat.send({
12 chatRequest: {
13 messages: [
14 {
15 content: "You are a helpful assistant.",
16 role: "system",
17 },
18 {
19 content: "What is the capital of France?",
20 role: "user",
21 },
22 ],
23 },
24 });
25
26 console.log(result);
27}
28
29run();

Standalone function

The standalone function version of this method:

1import { OpenRouterCore } from "@openrouter/sdk/core.js";
2import { chatSend } from "@openrouter/sdk/funcs/chatSend.js";
3
4// Use `OpenRouterCore` for best tree-shaking performance.
5// You can create one instance of it to use across an application.
6const openRouter = new OpenRouterCore({
7 httpReferer: "<value>",
8 appTitle: "<value>",
9 appCategories: "<value>",
10 apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
11});
12
13async function run() {
14 const res = await chatSend(openRouter, {
15 chatRequest: {
16 messages: [
17 {
18 content: "You are a helpful assistant.",
19 role: "system",
20 },
21 {
22 content: "What is the capital of France?",
23 role: "user",
24 },
25 ],
26 },
27 });
28 if (res.ok) {
29 const { value: result } = res;
30 console.log(result);
31 } else {
32 console.log("chatSend failed:", res.error);
33 }
34}
35
36run();

Example Usage: insufficient-permissions

1import { OpenRouter } from "@openrouter/sdk";
2
3const openRouter = new OpenRouter({
4 httpReferer: "<value>",
5 appTitle: "<value>",
6 appCategories: "<value>",
7 apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
8});
9
10async function run() {
11 const result = await openRouter.chat.send({
12 chatRequest: {
13 messages: [
14 {
15 content: "You are a helpful assistant.",
16 role: "system",
17 },
18 {
19 content: "What is the capital of France?",
20 role: "user",
21 },
22 ],
23 },
24 });
25
26 console.log(result);
27}
28
29run();

Standalone function

The standalone function version of this method:

1import { OpenRouterCore } from "@openrouter/sdk/core.js";
2import { chatSend } from "@openrouter/sdk/funcs/chatSend.js";
3
4// Use `OpenRouterCore` for best tree-shaking performance.
5// You can create one instance of it to use across an application.
6const openRouter = new OpenRouterCore({
7 httpReferer: "<value>",
8 appTitle: "<value>",
9 appCategories: "<value>",
10 apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
11});
12
13async function run() {
14 const res = await chatSend(openRouter, {
15 chatRequest: {
16 messages: [
17 {
18 content: "You are a helpful assistant.",
19 role: "system",
20 },
21 {
22 content: "What is the capital of France?",
23 role: "user",
24 },
25 ],
26 },
27 });
28 if (res.ok) {
29 const { value: result } = res;
30 console.log(result);
31 } else {
32 console.log("chatSend failed:", res.error);
33 }
34}
35
36run();

Parameters

ParameterTypeRequiredDescription
requestoperations.SendChatCompletionRequestRequest✔️The request object to use for the request.
optionsRequestOptions➖Used to set various options for making HTTP requests.
options.fetchOptionsRequestInit➖Options that are passed to the underlying HTTP request. This can be used to inject extra headers for examples. All Request options, except method and body, are allowed.
options.retriesRetryConfig➖Enables retrying HTTP requests under certain failure conditions.

Response

Promise<operations.SendChatCompletionRequestResponse>

Errors

Error TypeStatus CodeContent Type
errors.BadRequestResponseError400application/json
errors.UnauthorizedResponseError401application/json
errors.PaymentRequiredResponseError402application/json
errors.ForbiddenResponseError403application/json
errors.NotFoundResponseError404application/json
errors.RequestTimeoutResponseError408application/json
errors.PayloadTooLargeResponseError413application/json
errors.UnprocessableEntityResponseError422application/json
errors.TooManyRequestsResponseError429application/json
errors.InternalServerResponseError500application/json
errors.BadGatewayResponseError502application/json
errors.ServiceUnavailableResponseError503application/json
errors.EdgeNetworkTimeoutResponseError524application/json
errors.ProviderOverloadedResponseError529application/json
errors.OpenRouterDefaultError4XX, 5XX*/*