Models - TypeScript SDK

Models method reference

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

(models)

Overview

Model information endpoints

Available Operations

  • count - Get total count of available models
  • list - List all models and their properties
  • listForUser - List models filtered by user provider preferences

count

Get total count of available models

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.models.count();
9
10 console.log(result);
11}
12
13run();

Standalone function

The standalone function version of this method:

1import { OpenRouterCore } from "@openrouter/sdk/core.js";
2import { modelsCount } from "@openrouter/sdk/funcs/modelsCount.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 modelsCount(openRouter);
12 if (res.ok) {
13 const { value: result } = res;
14 console.log(result);
15 } else {
16 console.log("modelsCount failed:", res.error);
17 }
18}
19
20run();

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 // Query hooks for fetching data.
3 useModelsCount,
4 useModelsCountSuspense,
5
6 // Utility for prefetching data during server-side rendering and in React
7 // Server Components that will be immediately available to client components
8 // using the hooks.
9 prefetchModelsCount,
10
11 // Utility to invalidate the query cache for this query in response to
12 // mutations and other user actions.
13 invalidateAllModelsCount,
14} from "@openrouter/sdk/react-query/modelsCount.js";

Parameters

ParameterTypeRequiredDescription
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.ModelsCountResponse>

Errors

Error TypeStatus CodeContent Type
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*

list

List all models and their properties

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.models.list();
9
10 console.log(result);
11}
12
13run();

Standalone function

The standalone function version of this method:

1import { OpenRouterCore } from "@openrouter/sdk/core.js";
2import { modelsList } from "@openrouter/sdk/funcs/modelsList.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 modelsList(openRouter);
12 if (res.ok) {
13 const { value: result } = res;
14 console.log(result);
15 } else {
16 console.log("modelsList failed:", res.error);
17 }
18}
19
20run();

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 // Query hooks for fetching data.
3 useModelsList,
4 useModelsListSuspense,
5
6 // Utility for prefetching data during server-side rendering and in React
7 // Server Components that will be immediately available to client components
8 // using the hooks.
9 prefetchModelsList,
10
11 // Utilities to invalidate the query cache for this query in response to
12 // mutations and other user actions.
13 invalidateModelsList,
14 invalidateAllModelsList,
15} from "@openrouter/sdk/react-query/modelsList.js";

Parameters

ParameterTypeRequiredDescription
requestoperations.GetModelsRequest✔️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.ModelsListResponse>

Errors

Error TypeStatus CodeContent Type
errors.BadRequestResponseError400application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*

listForUser

List models filtered by user provider preferences

Example Usage

1import { OpenRouter } from "@openrouter/sdk";
2
3const openRouter = new OpenRouter();
4
5async function run() {
6 const result = await openRouter.models.listForUser({
7 bearer: process.env["OPENROUTER_BEARER"] ?? "",
8 });
9
10 console.log(result);
11}
12
13run();

Standalone function

The standalone function version of this method:

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

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 // Query hooks for fetching data.
3 useModelsListForUser,
4 useModelsListForUserSuspense,
5
6 // Utility for prefetching data during server-side rendering and in React
7 // Server Components that will be immediately available to client components
8 // using the hooks.
9 prefetchModelsListForUser,
10
11 // Utility to invalidate the query cache for this query in response to
12 // mutations and other user actions.
13 invalidateAllModelsListForUser,
14} from "@openrouter/sdk/react-query/modelsListForUser.js";

Parameters

ParameterTypeRequiredDescription
securityoperations.ListModelsUserSecurity✔️The security requirements 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.ModelsListResponse>

Errors

Error TypeStatus CodeContent Type
errors.UnauthorizedResponseError401application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*