Files - TypeScript SDK
Files - TypeScript SDK
The TypeScript SDK and docs are currently in beta. Report issues on GitHub.
Overview
Files endpoints
Available Operations
- list - List files
- upload - Upload a file
- delete - Delete a file
- retrieve - Get file metadata
- download - Download file content
list
Lists files belonging to the workspace of the authenticating API key.
Example Usage
1 import { OpenRouter } from "@openrouter/sdk"; 2 3 const openRouter = new OpenRouter({ 4 httpReferer: "<value>", 5 appTitle: "<value>", 6 appCategories: "<value>", 7 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 8 }); 9 10 async function run() { 11 const result = await openRouter.files.list(); 12 13 for await (const page of result) { 14 console.log(page); 15 } 16 } 17 18 run();
Standalone function
The standalone function version of this method:
1 import { OpenRouterCore } from "@openrouter/sdk/core.js"; 2 import { filesList } from "@openrouter/sdk/funcs/filesList.js"; 3 4 // Use `OpenRouterCore` for best tree-shaking performance. 5 // You can create one instance of it to use across an application. 6 const openRouter = new OpenRouterCore({ 7 httpReferer: "<value>", 8 appTitle: "<value>", 9 appCategories: "<value>", 10 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 11 }); 12 13 async function run() { 14 const res = await filesList(openRouter); 15 if (res.ok) { 16 const { value: result } = res; 17 for await (const page of result) { 18 console.log(page); 19 } 20 } else { 21 console.log("filesList failed:", res.error); 22 } 23 } 24 25 run();
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | operations.ListFilesRequest | ✔️ | The request object to use for the request. |
options | RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions | RequestInit | ➖ | 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.retries | RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Response
Promise<operations.ListFilesResponse>
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.BadRequestResponseError | 400 | application/json |
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
upload
Uploads a file to be referenced in future API calls. The file is stored under the workspace of the authenticating API key. Maximum file size: 100 MB.
Example Usage
1 import { OpenRouter } from "@openrouter/sdk"; 2 import { openAsBlob } from "node:fs"; 3 4 const openRouter = new OpenRouter({ 5 httpReferer: "<value>", 6 appTitle: "<value>", 7 appCategories: "<value>", 8 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 9 }); 10 11 async function run() { 12 const result = await openRouter.files.upload({ 13 requestBody: { 14 file: await openAsBlob("example.file"), 15 }, 16 }); 17 18 console.log(result); 19 } 20 21 run();
Standalone function
The standalone function version of this method:
1 import { OpenRouterCore } from "@openrouter/sdk/core.js"; 2 import { filesUpload } from "@openrouter/sdk/funcs/filesUpload.js"; 3 import { openAsBlob } from "node:fs"; 4 5 // Use `OpenRouterCore` for best tree-shaking performance. 6 // You can create one instance of it to use across an application. 7 const openRouter = new OpenRouterCore({ 8 httpReferer: "<value>", 9 appTitle: "<value>", 10 appCategories: "<value>", 11 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 12 }); 13 14 async function run() { 15 const res = await filesUpload(openRouter, { 16 requestBody: { 17 file: await openAsBlob("example.file"), 18 }, 19 }); 20 if (res.ok) { 21 const { value: result } = res; 22 console.log(result); 23 } else { 24 console.log("filesUpload failed:", res.error); 25 } 26 } 27 28 run();
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | operations.UploadFileRequest | ✔️ | The request object to use for the request. |
options | RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions | RequestInit | ➖ | 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.retries | RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Response
Promise<models.FileMetadata>
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.BadRequestResponseError | 400 | application/json |
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.ForbiddenResponseError | 403 | application/json |
| errors.PayloadTooLargeResponseError | 413 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
delete
Deletes a file owned by the requesting workspace. Deletion is irreversible.
Example Usage
1 import { OpenRouter } from "@openrouter/sdk"; 2 3 const openRouter = new OpenRouter({ 4 httpReferer: "<value>", 5 appTitle: "<value>", 6 appCategories: "<value>", 7 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 8 }); 9 10 async function run() { 11 const result = await openRouter.files.delete({ 12 fileId: "file_011CNha8iCJcU1wXNR6q4V8w", 13 }); 14 15 console.log(result); 16 } 17 18 run();
Standalone function
The standalone function version of this method:
1 import { OpenRouterCore } from "@openrouter/sdk/core.js"; 2 import { filesDelete } from "@openrouter/sdk/funcs/filesDelete.js"; 3 4 // Use `OpenRouterCore` for best tree-shaking performance. 5 // You can create one instance of it to use across an application. 6 const openRouter = new OpenRouterCore({ 7 httpReferer: "<value>", 8 appTitle: "<value>", 9 appCategories: "<value>", 10 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 11 }); 12 13 async function run() { 14 const res = await filesDelete(openRouter, { 15 fileId: "file_011CNha8iCJcU1wXNR6q4V8w", 16 }); 17 if (res.ok) { 18 const { value: result } = res; 19 console.log(result); 20 } else { 21 console.log("filesDelete failed:", res.error); 22 } 23 } 24 25 run();
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | operations.DeleteFileRequest | ✔️ | The request object to use for the request. |
options | RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions | RequestInit | ➖ | 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.retries | RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Response
Promise<models.FileDeleteResponse>
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.NotFoundResponseError | 404 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
retrieve
Retrieves metadata for a single file owned by the requesting workspace.
Example Usage
1 import { OpenRouter } from "@openrouter/sdk"; 2 3 const openRouter = new OpenRouter({ 4 httpReferer: "<value>", 5 appTitle: "<value>", 6 appCategories: "<value>", 7 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 8 }); 9 10 async function run() { 11 const result = await openRouter.files.retrieve({ 12 fileId: "file_011CNha8iCJcU1wXNR6q4V8w", 13 }); 14 15 console.log(result); 16 } 17 18 run();
Standalone function
The standalone function version of this method:
1 import { OpenRouterCore } from "@openrouter/sdk/core.js"; 2 import { filesRetrieve } from "@openrouter/sdk/funcs/filesRetrieve.js"; 3 4 // Use `OpenRouterCore` for best tree-shaking performance. 5 // You can create one instance of it to use across an application. 6 const openRouter = new OpenRouterCore({ 7 httpReferer: "<value>", 8 appTitle: "<value>", 9 appCategories: "<value>", 10 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 11 }); 12 13 async function run() { 14 const res = await filesRetrieve(openRouter, { 15 fileId: "file_011CNha8iCJcU1wXNR6q4V8w", 16 }); 17 if (res.ok) { 18 const { value: result } = res; 19 console.log(result); 20 } else { 21 console.log("filesRetrieve failed:", res.error); 22 } 23 } 24 25 run();
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | operations.GetFileMetadataRequest | ✔️ | The request object to use for the request. |
options | RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions | RequestInit | ➖ | 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.retries | RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Response
Promise<models.FileMetadata>
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.NotFoundResponseError | 404 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
download
Downloads the raw bytes of a file. Only files created server-side are downloadable; uploaded files return 400.
Example Usage
1 import { OpenRouter } from "@openrouter/sdk"; 2 3 const openRouter = new OpenRouter({ 4 httpReferer: "<value>", 5 appTitle: "<value>", 6 appCategories: "<value>", 7 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 8 }); 9 10 async function run() { 11 const result = await openRouter.files.download({ 12 fileId: "file_011CNha8iCJcU1wXNR6q4V8w", 13 }); 14 15 console.log(result); 16 } 17 18 run();
Standalone function
The standalone function version of this method:
1 import { OpenRouterCore } from "@openrouter/sdk/core.js"; 2 import { filesDownload } from "@openrouter/sdk/funcs/filesDownload.js"; 3 4 // Use `OpenRouterCore` for best tree-shaking performance. 5 // You can create one instance of it to use across an application. 6 const openRouter = new OpenRouterCore({ 7 httpReferer: "<value>", 8 appTitle: "<value>", 9 appCategories: "<value>", 10 apiKey: process.env["OPENROUTER_API_KEY"] ?? "", 11 }); 12 13 async function run() { 14 const res = await filesDownload(openRouter, { 15 fileId: "file_011CNha8iCJcU1wXNR6q4V8w", 16 }); 17 if (res.ok) { 18 const { value: result } = res; 19 console.log(result); 20 } else { 21 console.log("filesDownload failed:", res.error); 22 } 23 } 24 25 run();
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | operations.DownloadFileContentRequest | ✔️ | The request object to use for the request. |
options | RequestOptions | ➖ | Used to set various options for making HTTP requests. |
options.fetchOptions | RequestInit | ➖ | 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.retries | RetryConfig | ➖ | Enables retrying HTTP requests under certain failure conditions. |
Response
Promise<ReadableStream<Uint8Array>>
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.BadRequestResponseError | 400 | application/json |
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.NotFoundResponseError | 404 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |