Use Go SDKs generated in Postman
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.
SDK structure
The Go SDK includes the following key files:
pkg/[sdkname]/[sdkname].go— The main SDK client, instantiated with aConfig.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—Environmenttype and server constants.pkg/shared/[sdkname]_response.go— Response wrappers ([SdkName]Response[T],[SdkName]Error[T]).pkg/util/to_pointer.go— TheToPointerhelper.pkg/util/nullable.go— TheNullable[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:
Example usage
Each generated SDK includes a cmd/examples/example.go file demonstrating SDK initialization, making an API call, and basic error handling.
Location
Run the example
Example code structure
cmd/examples/example.go
Install the Go SDK
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.
Install using go get
To install the SDK and add it to your module, run:
Then import it in your code:
Install from a local path
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.
Publish to a Go module registry
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.
Prerequisites
To publish your SDK, you need the following:
- A public Git repository (for example, GitHub or GitLab).
- The module path in
go.modmust match the repository URL (for example,module github.com/your-org/your-sdk).
Publish your Go 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.
Publish updates
To publish updates to your SDK, run the following:
Best practices for publishing Go SDKs
Use the following best practices when publishing your Go SDK:
- Use semantic versioning with major version bumps for breaking changes.
- For v2 and later, update the module path in
go.modto include the major version suffix (for example,module github.com/your-org/your-sdk/v2). - Write Go doc comments on all exported types and functions. They become the SDK’s documentation on Go Packages.
- Tag releases in Git rather than publishing snapshots.
Advanced usage
Authentication
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.
API Key
Bearer Token
Basic Auth
OAuth 2.0
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.
Error handling
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:
Environment management
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.
Hierarchical configuration
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:
Timeouts and retries
To set a global timeout, run the following:
To configure retries through RetryConfig, run the following:
The default retry configuration is:
Validation
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.
Optional and nullable field handling
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:
Additional resources
Consider the following resources for using and customizing your Go SDK:
- SDK documentation — Check the
documentation/directory inside your SDK for per-service and per-model reference pages. - Go doc — Run
go doc ./...in your SDK directory to browse documentation in the terminal. - Go Packages — Once published, your SDK documentation is available at
https://pkg.go.dev/github.com/your-org/your-sdk. - Example usage — See
cmd/examples/example.gofor a runnable demonstration of the SDK. - Dependencies — The SDK uses only the Go standard library and requires
Go >= 1.19.0.