1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package anthropic
4
5import (
6 "context"
7 "net/http"
8 "os"
9
10 "github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
11 "github.com/anthropics/anthropic-sdk-go/option"
12)
13
14// Client creates a struct with services and top level methods that help with
15// interacting with the anthropic API. You should not instantiate this client
16// directly, and instead use the [NewClient] method instead.
17type Client struct {
18 Options []option.RequestOption
19 Completions CompletionService
20 Messages MessageService
21 Models ModelService
22 Beta BetaService
23}
24
25// DefaultClientOptions read from the environment (ANTHROPIC_API_KEY,
26// ANTHROPIC_AUTH_TOKEN, ANTHROPIC_BASE_URL). This should be used to initialize new
27// clients.
28func DefaultClientOptions() []option.RequestOption {
29 defaults := []option.RequestOption{option.WithEnvironmentProduction()}
30 if o, ok := os.LookupEnv("ANTHROPIC_BASE_URL"); ok {
31 defaults = append(defaults, option.WithBaseURL(o))
32 }
33 if o, ok := os.LookupEnv("ANTHROPIC_API_KEY"); ok {
34 defaults = append(defaults, option.WithAPIKey(o))
35 }
36 if o, ok := os.LookupEnv("ANTHROPIC_AUTH_TOKEN"); ok {
37 defaults = append(defaults, option.WithAuthToken(o))
38 }
39 return defaults
40}
41
42// NewClient generates a new client with the default option read from the
43// environment (ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, ANTHROPIC_BASE_URL). The
44// option passed in as arguments are applied after these default arguments, and all
45// option will be passed down to the services and requests that this client makes.
46func NewClient(opts ...option.RequestOption) (r Client) {
47 opts = append(DefaultClientOptions(), opts...)
48
49 r = Client{Options: opts}
50
51 r.Completions = NewCompletionService(opts...)
52 r.Messages = NewMessageService(opts...)
53 r.Models = NewModelService(opts...)
54 r.Beta = NewBetaService(opts...)
55
56 return
57}
58
59// Execute makes a request with the given context, method, URL, request params,
60// response, and request options. This is useful for hitting undocumented endpoints
61// while retaining the base URL, auth, retries, and other options from the client.
62//
63// If a byte slice or an [io.Reader] is supplied to params, it will be used as-is
64// for the request body.
65//
66// The params is by default serialized into the body using [encoding/json]. If your
67// type implements a MarshalJSON function, it will be used instead to serialize the
68// request. If a URLQuery method is implemented, the returned [url.Values] will be
69// used as query strings to the url.
70//
71// If your params struct uses [param.Field], you must provide either [MarshalJSON],
72// [URLQuery], and/or [MarshalForm] functions. It is undefined behavior to use a
73// struct uses [param.Field] without specifying how it is serialized.
74//
75// Any "β¦Params" object defined in this library can be used as the request
76// argument. Note that 'path' arguments will not be forwarded into the url.
77//
78// The response body will be deserialized into the res variable, depending on its
79// type:
80//
81// - A pointer to a [*http.Response] is populated by the raw response.
82// - A pointer to a byte array will be populated with the contents of the request
83// body.
84// - A pointer to any other type uses this library's default JSON decoding, which
85// respects UnmarshalJSON if it is defined on the type.
86// - A nil value will not read the response body.
87//
88// For even greater flexibility, see [option.WithResponseInto] and
89// [option.WithResponseBodyInto].
90func (r *Client) Execute(ctx context.Context, method string, path string, params any, res any, opts ...option.RequestOption) error {
91 opts = append(r.Options, opts...)
92 return requestconfig.ExecuteNewRequest(ctx, method, path, params, res, opts...)
93}
94
95// Get makes a GET request with the given URL, params, and optionally deserializes
96// to a response. See [Execute] documentation on the params and response.
97func (r *Client) Get(ctx context.Context, path string, params any, res any, opts ...option.RequestOption) error {
98 return r.Execute(ctx, http.MethodGet, path, params, res, opts...)
99}
100
101// Post makes a POST request with the given URL, params, and optionally
102// deserializes to a response. See [Execute] documentation on the params and
103// response.
104func (r *Client) Post(ctx context.Context, path string, params any, res any, opts ...option.RequestOption) error {
105 return r.Execute(ctx, http.MethodPost, path, params, res, opts...)
106}
107
108// Put makes a PUT request with the given URL, params, and optionally deserializes
109// to a response. See [Execute] documentation on the params and response.
110func (r *Client) Put(ctx context.Context, path string, params any, res any, opts ...option.RequestOption) error {
111 return r.Execute(ctx, http.MethodPut, path, params, res, opts...)
112}
113
114// Patch makes a PATCH request with the given URL, params, and optionally
115// deserializes to a response. See [Execute] documentation on the params and
116// response.
117func (r *Client) Patch(ctx context.Context, path string, params any, res any, opts ...option.RequestOption) error {
118 return r.Execute(ctx, http.MethodPatch, path, params, res, opts...)
119}
120
121// Delete makes a DELETE request with the given URL, params, and optionally
122// deserializes to a response. See [Execute] documentation on the params and
123// response.
124func (r *Client) Delete(ctx context.Context, path string, params any, res any, opts ...option.RequestOption) error {
125 return r.Execute(ctx, http.MethodDelete, path, params, res, opts...)
126}