Build Go SDKs for your API
Tags: openapi, sdk, generator, go
Published: 2024-01-25 14:42:00

We’re APIMatic.io. We generate strongly typed SDKs along with a complete API reference from OpenAPI definitions. If you want to create an SDK for your REST API, check out how to [generate one in under a minute](https://www.apimatic.io/blog/2022/08/how-to-create-sdks-for-your-api-in-less-than-a-minute). We support generating SDKs for C#, Java, PHP, TypeScript, Python, Ruby, and now Golang using the APIMatic Code Generator.

We are excited to announce that our new Go SDK Code Generator is now available as an alpha release. According to the StackOverflow [Developer Survey 2023](https://survey.stackoverflow.co/2023/#most-popular-technologies-language-prof), Go is used by ~14% of professional developers worldwide and is ranked amongst the top most admired languages by developers. Developers appreciate Go for its [simplicity](https://www.apimatic.io/blog/2023/04/how-gos-simplicity-brings-complexity), speed, cross-compilation support, concurrency primitives, and expansive standard library. Go developers commonly use it to create distributed network applications, cloud-native services, and standalone tools and utilities. Using the new APIMatic Code Generator, API Providers can now make their APIs accessible to Go developers by generating strongly typed, idiomatic Go SDKs for their API.


In this blog, we will take a look at the design of the Go SDKs generated by APIMatic. We will especially focus on how they help improve the developer experience, reduce onboarding time, and simplify integration code for API developers. We will also check why APIMatic’s Code Generation solution, especially for Go, stands out among the [various options for creating SDKs](https://sdks.io/docs/ways-to-build-sdks/?__hstc=245086810.2dd793be0254e8d39520772b95aca4c2.1699388458843.1705693818058.1705696127258.116&__hssc=245086810.5.1705696127258&__hsfp=3857295638).

## A Quick Tour of the Go SDK
Let’s check out what the new Go SDKs generated by APIMatic Code Generator look like. These examples are from an SDK generated for a model payment API we call IdealPay and help illustrate the Go SDK usage experience.

### Making an API Call
The Go SDK wraps the API into a Go-native interface while hiding away all the nitty gritty details of making an API call. JSON request and response bodies become Go structs. API calls become method calls. Authentication and error handling are built into the SDK and require minimal effort on the part of the developer to use. All this adds up to a developer experience that reduces the need for the Go developer to go back and forth between the API reference and the code editor as they figure out how to integrate the API into their application.

Let's check out a code snippet that shows an API call being made using the Go SDK:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbegzsp29t98msia6e6t.png)

APIMatic takes a batteries-included approach to the Go SDK design, reducing the effort needed by the API developers to write production-ready code by providing commonly used features such as:

- **Concurrency Support**: The client is designed to be immutable. This allows the client instance to be shared between multiple threads without running into race conditions.
- **Timeout and Retries**: The SDK is designed to be resilient. Intermittent API call failures are handled by retrying the API calls based on an exponential backup algorithm. Non-responsive HTTP calls are timed out safely to release resources.
- **Context-Aware Calls**: The SDK allows the caller to pass “context”. This allows the SDK to be used in network applications where an API call might need to be canceled whenever a parent request is canceled.

### Working with Request and Response Data
The generated Go SDK provides a strongly typed interface for making API calls by providing Go types for all API requests and responses. In the Go language, this means providing structs, enums, and interface types that help the developers provide the right data to the SDK when making API calls. The Go SDK then converts this data into the right API call by serializing the data as JSON, XML, or form data. However, the developers are oblivious to this detail as the SDK does the heavy lifting of creating the right API call saving the developer from having to read the API reference docs.

The code snippet below shows the request data being passed to the SDK during an API call. Check out how the Go SDK exposes a neat Go-native interface for handling various request data types:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr1krl83a9ynmo526ug9.png)

Since the response data is also strongly typed, the Go developer can quickly introspect the response type using their IDE’s auto-complete or type-hint functionality. Check out how VS Code hints at the available properties in the `PaymentLink` response:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvtjzhlf97j6euilgx9u.png)

Strong typing in both request and response helps catch code errors early in the development process by restricting the data types that can be exchanged with the SDK. The compiler raises errors during development which helps the developer correct mistakes, thereby eliminating an entire class of bugs from reaching the production environment.

### Documentation and Code Samples
APIMatic not only generates the Go SDK but also supports creating a complete SDK Reference documentation in the shape of a website. This website contains the following for Go SDK and other generated SDKs:

- Installation and usage instructions for the SDK
- Endpoint reference
- Request and response type reference
- ⭐ Code samples showing API calls using Go SDK
- ⭐ API Console to make API calls without leaving the website

The screenshot below shows a reference documentation site that we have created for a Petstore API, showing documentation for the “Add Pet” endpoint using the Go SDK:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbpc7r7x6iyfmx9obgjs.png)

Developers can copy the code samples from the documentation into their IDE to quickly integrate the API call into their application. Since these code samples use the Go SDK, they are DRY ([Don’t repeat yourself](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)) because they do not embed the entire API call implementation detail inside the code samples; that implementation along with the types needed for requests and responses are provided by the Go SDK.

While inside the IDE, developers benefit from the IDE “Intellisense” features which help surface the method, types, and documentation for the SDK. This helps speed up the build-and-test development loop for developers, improving developer experience:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owk4m9odon4xf4wynioz.png)

## Why Choose APIMatic’s Code Generator for Go SDKs?
APIMatic’s newest support for the Go language helps advance our goal of building the most comprehensive platform for crafting API developer experiences. With 8 years of development and millions of SDK downloads under its belt, APIMatic Code Generator represents the most mature solution for API providers to [create SDKs for their API Programs](https://www.apimatic.io/resource/modern-guide-to-building-sdks).

Here’s why we think our newest Code Generator for Go SDKs stands out from the crowd:

- **Designed from scratch for Go**: APIMatic takes a principled approach to SDK design focusing on creating strongly typed, [idiomatic SDKs](https://sdks.io/docs/best-practices/design/idiomatic-code/?__hstc=245086810.2dd793be0254e8d39520772b95aca4c2.1699388458843.1705693818058.1705696127258.116&__hssc=245086810.19.1705696127258&__hsfp=3857295638) that make the developers feel at home. Go developers will appreciate the attention to detail we have put into crafting neat types and interfaces in the SDK aligned with Go’s language conventions. This includes the use of the new Generics feature to simplify interfaces, the presence of doc blocks to types and methods, and the locking of dependencies using Go modules.
- **Strong OpenAPI Coverage**: APIMatic’s Code Generator has expansive support for OpenAPI features, including polymorphic inheritance, multipart requests containing files, nullable and optional types, and authentication schemes such as OAuth 2. Our API Validator helps you get the most out of your OpenAPI definition by finding common issues including hard-to-detect semantic errors.
- **Zero-maintenance SDKs**: APIMatic’s approach to SDK generation helps significantly reduce the cost of ownership of SDKs and docs for API providers. Once you’ve published your SDKs, APIMatic will automatically provide updates and security fixes via the [Core Libraries](https://www.apimatic.io/blog/2023/06/core-libraries-in-sdks) that power our SDKs. Add APIMatic’s [documentation solution](https://www.apimatic.io/solution/no-code-dx) and you have API and SDK references that stay up-to-date with your latest API definition changes.

Just like this exciting new release, you can always depend on the APIMatic team to deliver awesome improvements to the SDKs and supporting API tools. Our recent releases include:

- Complete [overhaul of the core functionality](https://www.apimatic.io/blog/2023/06/core-libraries-in-sdks) of SDKs resulting in leaner code and easier maintenance.
- New documentation for [Code Generation Settings](https://www.apimatic.io/blog/2023/06/codegen-settings-docs-got-a-makeover) to help you discover all the ways you can customize your SDKs.
- [Integration guide for Readme users](https://www.apimatic.io/blog/2023/05/sdks-for-readme-users) to add SDKs to their documentation sites.
- A [community for SDK creators](https://www.apimatic.io/blog/2023/04/say-hello-to-sdks-io-a-resource-for-sdk-builders) and developers to learn best practices for building SDKs.
- **Coming soon**: Expanding support for complex OpenAPI schemas like OneOf and AnyOf.
- **Coming soon**: Ability to fix OpenAPI definitions automatically.

So what are you waiting for? Ready, set, Golang! 🎉

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc0orzpti783ll5jarxw.png)

You can sign up for a [free 14-day trial of APIMatic](https://hubs.ly/Q02h50ry0) to generate a Go SDK for your API. During the trial, you can generate SDKs and API Documentation in all programming languages supported by the APIMatic platform.