Install and use the Qoder Cloud Agents Go SDK, including authentication, request types, streaming responses, pagination, retries, and timeouts.
The Qoder Cloud Agents Go SDK provides typed Forward and Managed clients. It supports request control through
Requires Go 1.23 or later. Install it in an existing Go Modules project:
Keep
Save your personal access token (PAT) in an environment variable:
Use the Forward client to create a template and identity, start a session, and send a message:
Use the Managed client to create an Agent and environment, start a session, and send a message. Save the following code as
Subsequent Go snippets omit
Pass configuration to constructors or individual calls through the
Explicit configuration takes precedence over environment variables. Default PAT mode reads
Use
After setting
Both clients share the same credential instance and token cache. For the China site, use both
Required fields generally use Go values directly; optional scalars use
The following function demonstrates parameter encoding only; it does not make a request:
Whether a field accepts
For request union types, select one representation through an
Use
A single stream ends at EOF. To keep subscribing after a recoverable disconnection, explicitly use
A resumable stream reconnects with the SSE cursor of the most recently delivered event, without resending business requests. It may run continuously; use
HTTP errors use
Errors also provide
By default, requests are retried up to 2 times, for a maximum of 3 attempts. Retries require a replayable request body and a caller
Within these safety rules, the SDK also handles server retry hints and delays; some APIs impose stricter retry limits. Use
The default Transport timeout for waiting for response headers is 10 minutes. This limit does not cover response body reads and is not a total deadline for the entire call.
Use
The example waits at most 1 minute overall and 15 seconds per attempt; whichever deadline arrives first takes effect.
Automatic pagination fetches subsequent pages on demand:
For page-by-page processing, use
Paginators retain query conditions and request configuration. Cursor formats can differ between APIs; prefer the pagination methods provided by the returned page.
Use
The response body may already have been read by the SDK. Use
Use
Use
context, reading streaming responses event by event, and configuring authentication, retries, and HTTP transport.
Installation
Requires Go 1.23 or later. Install it in an existing Go Modules project:
go.mod and go.sum in version control to pin the SDK version used by your application.
Basic usage
Save your personal access token (PAT) in an environment variable:
main.go and run go run main.go in a project with the SDK installed:
package and import. In addition to the standard library, convention, option, and param refer to the module packages convention, convention/option, and convention/param, respectively. In the examples, client is a configured Forward client; replace resource IDs with real IDs accessible to the current credentials.
See the Managed quickstart and Forward API reference for more integration instructions.
Client configuration
Pass configuration to constructors or individual calls through the option package. Per-call settings usually override client defaults for the same option; cumulative options such as appended headers and middleware are combined according to their own rules.
| Configuration | Forward | Managed |
|---|---|---|
| Constructor | forward.NewClient | managed.NewClient |
| Base URL environment variable | QODER_FORWARD_BASE_URL | QODER_BASE_URL |
| Global default URL | https://api.qoder.com/api/v1/forward | https://api.qoder.com/api/v1/cloud |
| CN URL | https://api.qoder.com.cn/api/v1/forward | https://api.qoder.com.cn/api/v1/cloud |
QODER_ACCESS_TOKEN first, then QODER_PAT. Clients do not automatically load .env files.
The Base URL must include the full API prefix and match the region of the account, credentials, and resources. Complete configuration before reusing a client across goroutines; do not modify its Options concurrently after sharing it.
Authentication
PAT
Use option.WithPAT(token) to explicitly configure a personal access token. Store the token in your application server environment.
Service Account
After setting QODER_SA_KEY, explicitly create a credential component. It exchanges and caches SATs (service account tokens), refreshing the token when subsequent requests require it:
Region: "cn" and a CN Base URL. For Managed-only access, you can request the qca.access scope; permissions and Managed workspace bindings must be configured on the server.
Ordinary clients do not automatically enable credential exchange merely because QODER_SA_KEY exists in the environment. An SA Key is only used to obtain SATs and cannot serve directly as a business API access token. SA authentication failures do not fall back to PAT.
If the application manages SATs itself, use option.WithAuthMode("sa") and option.WithAuthToken(token). The application must refresh static SATs. Choose one explicit credential source per client; avoid combining native SA credentials with static tokens. See the authentication documentation.
Request and response types
Optional fields
Required fields generally use Go values directly; optional scalars use param.Opt[T]. An unset field differs from an explicitly sent zero value:
| Expression | JSON behavior |
|---|---|
Unset param.Opt[T] | Omit the field |
forward.String("") | Send an empty string |
forward.Bool(false) | Send false |
forward.Int(0) | Send 0 |
param.Null[string]() | Send null |
Nil map or slice with omitzero | Omit the field |
| Non-nil empty map or slice | Send {} or [] |
null and how the server handles it are defined by the corresponding API. Managed provides equivalent helpers such as managed.String, Bool, and Int.
Union types and response fields
For request union types, select one representation through an Of... field and set only one branch. For response union types, use the provided As... or AsAny() methods to read a specific branch; see the API Reference for exact methods.
The JSON metadata in response structures distinguishes missing fields, explicit null, and normal values. Do not use Go zero values alone to determine whether a field exists. Use the response model RawJSON() for raw JSON. Unmodeled fields are retained in the response extra-field information.
Streaming responses
Use StreamEvents to establish a single SSE connection, read events in order, and check Err() afterward:
NewResumableStream:
context to set a subscription deadline or cancel explicitly. HTTP retry counts apply to individual connection requests, not the reconnection limit of the entire subscription.
Without a cursor, it starts at the current event stream tail. To read history, first obtain a valid historical resume position. Automatic reconnection does not guarantee exactly-once processing; multiple deltas for one event may share an ID, so do not discard all repeated frames solely by ID. See the corresponding API definitions for event meanings and task completion conditions.
Closing the stream or cancelling the local context stops only the current subscription; it does not automatically stop execution already started in the cloud.
Error handling and Request ID
HTTP errors use *convention.Error. Handle network, encoding, decoding, and context errors separately:
Message, Type(), Request, Response, and RawJSON(). Keep status codes, error codes, and Request IDs for troubleshooting; avoid logging complete requests containing access tokens.
Retries and idempotency
By default, requests are retried up to 2 times, for a maximum of 3 attempts. Retries require a replayable request body and a caller context that is still valid.
| Request | Retried by default for |
|---|---|
| GET, HEAD, or requests with an idempotency key | Connection errors, 408, 429, 5xx |
| Other requests without an idempotency key | 429 |
| 409 response | No automatic retries |
option.WithMaxRetries(0) to disable retries, or override the count for an individual call.
The application generates and stores idempotency keys. Pass one through the request parameter IdempotencyKey or option.WithHeader("Idempotency-Key", key). Reuse the same key for all retries of one logical operation; use a new key for a new operation.
Timeouts and cancellation
The default Transport timeout for waiting for response headers is 10 minutes. This limit does not cover response body reads and is not a total deadline for the entire call.
Use context to control the entire call and WithRequestTimeout to control each attempt:
WithRequestTimeout also covers response body reads, so configure a suitable subscription deadline for long-lived SSE.
Pagination
Automatic pagination fetches subsequent pages on demand:
List and GetNextPage(). When there is no next page, it returns nil:
Advanced configuration
Raw responses
Use WithResponseInto to read the status code and response headers:
RawJSON() on the parsed result for response JSON, or WithResponseBodyInto to specify your own decoding destination.
HTTP clients and request extensions
Use WithHTTPClient to configure connection pools, proxies, and a custom Transport. When you supply a custom client, the application is responsible for its timeouts and connection pool configuration.
WithMiddleware to observe each request attempt. Use WithHeader, WithQuery, and WithJSONSet for custom headers, query parameters, and JSON fields. The parameter structure SetExtraFields accepts fields already supported by the server but not yet modeled by the SDK. Override only fields your application explicitly needs to adjust, avoiding accidental replacement of authentication or routing configuration.