Files - Go SDK

Files method reference

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

Overview

Files endpoints

Available Operations

List

Lists files belonging to the workspace of the authenticating API key.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.Files.List(ctx, nil, nil, nil)
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 for {
23 // handle items
24
25 res, err = res.Next()
26
27 if err != nil {
28 // handle error
29 }
30
31 if res == nil {
32 break
33 }
34 }
35 }
36}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
limit*int64Maximum number of files to return (1–1000).100
cursor*stringOpaque pagination cursor from a previous response.eyJjdXJzb3IiOiJmaWxlXzAxMUNOaGE4aUNKY1Uxd1hOUjZxNFY4dyJ9
workspaceID*stringWorkspace to scope the request to. Defaults to the caller’s default workspace.a103d8b6-42f0-4e50-9a3c-bf41e2c3c1a7
opts[]operations.OptionThe options for this request.

Response

*operations.ListFilesResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.BadRequestResponseError400application/json
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 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

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/operations"
8 "log"
9)
10
11func main() {
12 ctx := context.Background()
13
14 s := openrouter.New(
15 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
16 )
17
18 example, fileErr := os.Open("example.file")
19 if fileErr != nil {
20 panic(fileErr)
21 }
22
23 res, err := s.Files.Upload(ctx, operations.UploadFileRequestBody{
24 File: operations.File{
25 FileName: "example.file",
26 Content: example,
27 },
28 }, nil)
29 if err != nil {
30 log.Fatal(err)
31 }
32 if res != nil {
33 // handle response
34 }
35}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
requestBodyoperations.UploadFileRequestBody✔️N/A
workspaceID*stringWorkspace to scope the request to. Defaults to the caller’s default workspace.a103d8b6-42f0-4e50-9a3c-bf41e2c3c1a7
opts[]operations.OptionThe options for this request.

Response

*components.FileMetadata, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.BadRequestResponseError400application/json
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.ForbiddenResponseError403application/json
sdkerrors.PayloadTooLargeResponseError413application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*

Delete

Deletes a file owned by the requesting workspace. Deletion is irreversible.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.Files.Delete(ctx, "file_011CNha8iCJcU1wXNR6q4V8w", nil)
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
fileIDstring✔️N/Afile_011CNha8iCJcU1wXNR6q4V8w
workspaceID*stringWorkspace to scope the request to. Defaults to the caller’s default workspace.a103d8b6-42f0-4e50-9a3c-bf41e2c3c1a7
opts[]operations.OptionThe options for this request.

Response

*components.FileDeleteResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.NotFoundResponseError404application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*

Retrieve

Retrieves metadata for a single file owned by the requesting workspace.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.Files.Retrieve(ctx, "file_011CNha8iCJcU1wXNR6q4V8w", nil)
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
fileIDstring✔️N/Afile_011CNha8iCJcU1wXNR6q4V8w
workspaceID*stringWorkspace to scope the request to. Defaults to the caller’s default workspace.a103d8b6-42f0-4e50-9a3c-bf41e2c3c1a7
opts[]operations.OptionThe options for this request.

Response

*components.FileMetadata, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.NotFoundResponseError404application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*

Download

Downloads the raw bytes of a file. Only files created server-side are downloadable; uploaded files return 400.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.Files.Download(ctx, "file_011CNha8iCJcU1wXNR6q4V8w", nil)
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
fileIDstring✔️N/Afile_011CNha8iCJcU1wXNR6q4V8w
workspaceID*stringWorkspace to scope the request to. Defaults to the caller’s default workspace.a103d8b6-42f0-4e50-9a3c-bf41e2c3c1a7
opts[]operations.OptionThe options for this request.

Response

io.ReadCloser, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.BadRequestResponseError400application/json
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.NotFoundResponseError404application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*