Completions - TypeScript SDK

Completions method reference

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

(completions)

Overview

Available Operations

generate

Creates a completion for the provided prompt and parameters. Supports both streaming and non-streaming modes.

Example Usage

1import { OpenRouter } from "@openrouter/sdk";
2
3const openRouter = new OpenRouter({
4 apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
5});
6
7async function run() {
8 const result = await openRouter.completions.generate({
9 prompt: "<value>",
10 });
11
12 console.log(result);
13}
14
15run();

Standalone function

The standalone function version of this method:

1import { OpenRouterCore } from "@openrouter/sdk/core.js";
2import { completionsGenerate } from "@openrouter/sdk/funcs/completionsGenerate.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 apiKey: process.env["OPENROUTER_API_KEY"] ?? "",
8});
9
10async function run() {
11 const res = await completionsGenerate(openRouter, {
12 prompt: "<value>",
13 });
14 if (res.ok) {
15 const { value: result } = res;
16 console.log(result);
17 } else {
18 console.log("completionsGenerate failed:", res.error);
19 }
20}
21
22run();

React hooks and utilities

This method can be used in React components through the following hooks and associated utilities.

Check out this guide for information about each of the utilities below and how to get started using React hooks.

1import {
2 // Mutation hook for triggering the API call.
3 useCompletionsGenerateMutation
4} from "@openrouter/sdk/react-query/completionsGenerate.js";

Parameters

ParameterTypeRequiredDescription
requestmodels.CompletionCreateParams✔️The request object to use for the request.
optionsRequestOptionsUsed to set various options for making HTTP requests.
options.fetchOptionsRequestInitOptions 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.retriesRetryConfigEnables retrying HTTP requests under certain failure conditions.

Response

Promise<models.CompletionResponse>

Errors

Error TypeStatus CodeContent Type
errors.ChatError400, 401, 429application/json
errors.ChatError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*