Guardrails - Go SDK

Guardrails method reference

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

Overview

Guardrails endpoints

Available Operations

List

List all guardrails for the authenticated user. Management key required.

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.Guardrails.List(ctx, 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.
offset*int64Number of records to skip for pagination0
limit*int64Maximum number of records to return (max 100)50
opts[]operations.OptionThe options for this request.

Response

*operations.ListGuardrailsResponse, error

Errors

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

Create

Create a new guardrail for the authenticated user. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/optionalnullable"
8 "github.com/OpenRouterTeam/go-sdk/models/components"
9 "log"
10)
11
12func main() {
13 ctx := context.Background()
14
15 s := openrouter.New(
16 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
17 )
18
19 res, err := s.Guardrails.Create(ctx, components.CreateGuardrailRequest{
20 Name: "My New Guardrail",
21 Description: optionalnullable.From(openrouter.Pointer("A guardrail for limiting API usage")),
22 LimitUsd: openrouter.Pointer[float64](50.0),
23 ResetInterval: optionalnullable.From(openrouter.Pointer(components.GuardrailIntervalMonthly)),
24 AllowedProviders: optionalnullable.From(openrouter.Pointer([]string{
25 "openai",
26 "anthropic",
27 "deepseek",
28 })),
29 IgnoredProviders: optionalnullable.From[[]string](nil),
30 AllowedModels: optionalnullable.From[[]string](nil),
31 EnforceZdr: optionalnullable.From(openrouter.Pointer(false)),
32 })
33 if err != nil {
34 log.Fatal(err)
35 }
36 if res != nil {
37 // handle response
38 }
39}

Parameters

ParameterTypeRequiredDescription
ctxcontext.Context✔️The context to use for the request.
requestcomponents.CreateGuardrailRequest✔️The request object to use for the request.
opts[]operations.OptionThe options for this request.

Response

*components.CreateGuardrailResponse, error

Errors

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

Get

Get a single guardrail by ID. Management key required.

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.Guardrails.Get(ctx, "550e8400-e29b-41d4-a716-446655440000")
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.
idstring✔️The unique identifier of the guardrail to retrieve550e8400-e29b-41d4-a716-446655440000
opts[]operations.OptionThe options for this request.

Response

*components.GetGuardrailResponse, error

Errors

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

Update

Update an existing guardrail. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/optionalnullable"
8 "github.com/OpenRouterTeam/go-sdk/models/components"
9 "log"
10)
11
12func main() {
13 ctx := context.Background()
14
15 s := openrouter.New(
16 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
17 )
18
19 res, err := s.Guardrails.Update(ctx, "550e8400-e29b-41d4-a716-446655440000", components.UpdateGuardrailRequest{
20 Name: openrouter.Pointer("Updated Guardrail Name"),
21 Description: optionalnullable.From(openrouter.Pointer("Updated description")),
22 LimitUsd: openrouter.Pointer[float64](75.0),
23 ResetInterval: optionalnullable.From(openrouter.Pointer(components.GuardrailIntervalWeekly)),
24 })
25 if err != nil {
26 log.Fatal(err)
27 }
28 if res != nil {
29 // handle response
30 }
31}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
idstring✔️The unique identifier of the guardrail to update550e8400-e29b-41d4-a716-446655440000
updateGuardrailRequestcomponents.UpdateGuardrailRequest✔️N/A{"name": "Updated Guardrail Name","description": "Updated description","limit_usd": 75,"reset_interval": "weekly"}
opts[]operations.OptionThe options for this request.

Response

*components.UpdateGuardrailResponse, error

Errors

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

Delete

Delete an existing guardrail. Management key required.

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.Guardrails.Delete(ctx, "550e8400-e29b-41d4-a716-446655440000")
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.
idstring✔️The unique identifier of the guardrail to delete550e8400-e29b-41d4-a716-446655440000
opts[]operations.OptionThe options for this request.

Response

*components.DeleteGuardrailResponse, error

Errors

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

ListKeyAssignments

List all API key guardrail assignments for the authenticated user. Management key required.

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.Guardrails.ListKeyAssignments(ctx, 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.
offset*int64Number of records to skip for pagination0
limit*int64Maximum number of records to return (max 100)50
opts[]operations.OptionThe options for this request.

Response

*operations.ListKeyAssignmentsResponse, error

Errors

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

ListMemberAssignments

List all organization member guardrail assignments for the authenticated user. Management key required.

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.Guardrails.ListMemberAssignments(ctx, 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.
offset*int64Number of records to skip for pagination0
limit*int64Maximum number of records to return (max 100)50
opts[]operations.OptionThe options for this request.

Response

*operations.ListMemberAssignmentsResponse, error

Errors

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

ListGuardrailKeyAssignments

List all API key assignments for a specific guardrail. Management key required.

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.Guardrails.ListGuardrailKeyAssignments(ctx, "550e8400-e29b-41d4-a716-446655440000", 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.
idstring✔️The unique identifier of the guardrail550e8400-e29b-41d4-a716-446655440000
offset*int64Number of records to skip for pagination0
limit*int64Maximum number of records to return (max 100)50
opts[]operations.OptionThe options for this request.

Response

*operations.ListGuardrailKeyAssignmentsResponse, error

Errors

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

BulkAssignKeys

Assign multiple API keys to a specific guardrail. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/components"
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 res, err := s.Guardrails.BulkAssignKeys(ctx, "550e8400-e29b-41d4-a716-446655440000", components.BulkAssignKeysRequest{
19 KeyHashes: []string{
20 "c56454edb818d6b14bc0d61c46025f1450b0f4012d12304ab40aacb519fcbc93",
21 },
22 })
23 if err != nil {
24 log.Fatal(err)
25 }
26 if res != nil {
27 // handle response
28 }
29}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
idstring✔️The unique identifier of the guardrail550e8400-e29b-41d4-a716-446655440000
bulkAssignKeysRequestcomponents.BulkAssignKeysRequest✔️N/A{"key_hashes": ["c56454edb818d6b14bc0d61c46025f1450b0f4012d12304ab40aacb519fcbc93"]}
opts[]operations.OptionThe options for this request.

Response

*components.BulkAssignKeysResponse, error

Errors

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

ListGuardrailMemberAssignments

List all organization member assignments for a specific guardrail. Management key required.

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.Guardrails.ListGuardrailMemberAssignments(ctx, "550e8400-e29b-41d4-a716-446655440000", 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.
idstring✔️The unique identifier of the guardrail550e8400-e29b-41d4-a716-446655440000
offset*int64Number of records to skip for pagination0
limit*int64Maximum number of records to return (max 100)50
opts[]operations.OptionThe options for this request.

Response

*operations.ListGuardrailMemberAssignmentsResponse, error

Errors

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

BulkAssignMembers

Assign multiple organization members to a specific guardrail. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/components"
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 res, err := s.Guardrails.BulkAssignMembers(ctx, "550e8400-e29b-41d4-a716-446655440000", components.BulkAssignMembersRequest{
19 MemberUserIds: []string{
20 "user_abc123",
21 "user_def456",
22 },
23 })
24 if err != nil {
25 log.Fatal(err)
26 }
27 if res != nil {
28 // handle response
29 }
30}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
idstring✔️The unique identifier of the guardrail550e8400-e29b-41d4-a716-446655440000
bulkAssignMembersRequestcomponents.BulkAssignMembersRequest✔️N/A{"member_user_ids": ["user_abc123","user_def456"]}
opts[]operations.OptionThe options for this request.

Response

*components.BulkAssignMembersResponse, error

Errors

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

BulkUnassignKeys

Unassign multiple API keys from a specific guardrail. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/components"
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 res, err := s.Guardrails.BulkUnassignKeys(ctx, "550e8400-e29b-41d4-a716-446655440000", components.BulkUnassignKeysRequest{
19 KeyHashes: []string{
20 "c56454edb818d6b14bc0d61c46025f1450b0f4012d12304ab40aacb519fcbc93",
21 },
22 })
23 if err != nil {
24 log.Fatal(err)
25 }
26 if res != nil {
27 // handle response
28 }
29}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
idstring✔️The unique identifier of the guardrail550e8400-e29b-41d4-a716-446655440000
bulkUnassignKeysRequestcomponents.BulkUnassignKeysRequest✔️N/A{"key_hashes": ["c56454edb818d6b14bc0d61c46025f1450b0f4012d12304ab40aacb519fcbc93"]}
opts[]operations.OptionThe options for this request.

Response

*components.BulkUnassignKeysResponse, error

Errors

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

BulkUnassignMembers

Unassign multiple organization members from a specific guardrail. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/components"
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 res, err := s.Guardrails.BulkUnassignMembers(ctx, "550e8400-e29b-41d4-a716-446655440000", components.BulkUnassignMembersRequest{
19 MemberUserIds: []string{
20 "user_abc123",
21 "user_def456",
22 },
23 })
24 if err != nil {
25 log.Fatal(err)
26 }
27 if res != nil {
28 // handle response
29 }
30}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
idstring✔️The unique identifier of the guardrail550e8400-e29b-41d4-a716-446655440000
bulkUnassignMembersRequestcomponents.BulkUnassignMembersRequest✔️N/A{"member_user_ids": ["user_abc123","user_def456"]}
opts[]operations.OptionThe options for this request.

Response

*components.BulkUnassignMembersResponse, error

Errors

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