This guide provides instructions for using Go SDKs generated by Postman SDK Generator. It covers how to install the SDK into your project, run example code, and best practices for using the SDK effectively in your applications.
The Go SDK includes the following key files:
pkg/[sdkname]/[sdkname].go — The main SDK client, instantiated with a Config.pkg/[servicename]/[servicename]_service.go — Service files with request methods.pkg/[servicename]/[model_name].go — Model structs and request parameter types.pkg/[sdkname]config/config.go — Configuration types (Config, RetryConfig, RequestOption).pkg/[sdkname]config/environments.go — Environment type and server constants.pkg/shared/[sdkname]_response.go — Response wrappers ([SdkName]Response[T], [SdkName]Error[T]).pkg/util/to_pointer.go — The ToPointer helper.pkg/util/nullable.go — The Nullable[T] type.internal/ — An internal HTTP transport and config management (not for direct use).go.mod — Go module definition.cmd/examples/example.go — Runnable example demonstrating SDK usage.The following is an example of the typical structure of a generated Go SDK:
Each generated SDK includes a cmd/examples/example.go file demonstrating SDK initialization, making an API call, and basic error handling.
cmd/examples/example.go
To use the generated Go SDK in your project, you can either install it from a public registry (like GitHub) or use a local path if it’s not yet published.
To install the SDK and add it to your module, run:
Then import it in your code:
If you are working with an SDK that is not yet published to a registry, use a replace directive in your go.mod to point to the local directory:
Then run go mod tidy to resolve dependencies.
Go modules are distributed through version control. Publishing your SDK makes it available using go get through the Go module proxy and pkg.go.dev.
To publish your SDK, you need the following:
go.mod must match the repository URL (for example, module github.com/your-org/your-sdk).To publish your SDK, do the following:
Push your SDK to a public Git repository.
Tag a release using semantic versioning.
The module is now available to any user using the following command:
Verify the publication at https://pkg.go.dev/github.com/your-org/your-sdk. Documentation is generated automatically from exported Go doc comments.
To publish updates to your SDK, run the following:
Use the following best practices when publishing your Go SDK:
go.mod to include the major version suffix (for example, module github.com/your-org/your-sdk/v2).The SDK supports various authentication methods. Configure authentication on the Config before constructing the SDK client.
The SDK includes authentication support based on the security schemes defined in your API specification. The following examples show the possible authentication methods.
To update credentials after initialization, call the same setter methods on the config. Each generated auth setter is only present when the corresponding auth type is defined in the specification.
Service methods return a pair of (*[SdkName]Response[T], *[SdkName]Error[E]). Exactly one of the two will be non-nil. Check the error before accessing the response.
The [SdkName]Error wrapper contains the following fields:
To handle specific status codes, run the following:
When the API specification defines typed error response models (for example, a NotFoundError schema on a 404 response), the SDK generates a corresponding model struct in the service package. Access its fields through the Data field of the error:
The SDK generates an Environment type with constants for each named server defined in your API specification.
Available environment constants are defined in pkg/[sdkname]config/environments.go.
Go supports four levels of configuration. Each level overrides the previous, with request-level being the most specific.
Available RequestOption constructors mirror the Config setters and are defined in pkg/[sdkname]config/config.go:
To set a global timeout, run the following:
To configure retries through RetryConfig, run the following:
The default retry configuration is:
The SDK generates validators that enforce OpenAPI constraints at runtime. Validation happens automatically when making API calls. The SDK validates parameters and request bodies before sending the request.
Supported constraints include: minLength, maxLength, pattern, minimum, maximum, multipleOf, minItems, maxItems, and uniqueItems.
If validation fails, the service method returns a non-nil error and the request isn’t sent:
No manual validation calls are needed.
The SDK uses two distinct types to represent optional and nullable fields, providing clear distinction between undefined, null, and present values. This is important when building request models, as omitting an optional field versus explicitly setting it to null produces different JSON payloads.
Optional non-nullable fields use Go pointer types (*T). Use util.ToPointer to create a pointer from a literal value:
Optional or required nullable fields use util.Nullable[T]. This wrapper explicitly distinguishes between absent, null, and valued states:
The following examples show the full range of field combinations:
Consider the following resources for using and customizing your Go SDK:
documentation/ directory inside your SDK for per-service and per-model reference pages.go doc ./... in your SDK directory to browse documentation in the terminal.https://pkg.go.dev/github.com/your-org/your-sdk.cmd/examples/example.go for a runnable demonstration of the SDK.Go >= 1.19.0.