Go SparkPost Library Released

Our Go client library is now publicly available!

We’ve been using it internally for various projects during development and now it’s time to go public. If you’re not familiar, “Go from Google” AKA Golang is a newer programming language, as these things, ahem, go. It’s been used to build a variety of publicly-available software including Docker and the Let’s Encrypt certificate authority. For a browser-based introduction to Go, check out A Tour of Go.

Go is statically typed and compiled, unlike most of the other languages that we have client libraries for. This makes the code a bit more verbose, and can make parsing JSON a bit frustrating at first. One of the goals of this library is to minimize that frustration during the “getting started with SparkPost and Go” phase.

So how do you use the SparkPost client library for Go? Once your Go workspace is set up, you can run:

go get github.com/SparkPost/gosparkpost

This will download the client library code so it’s ready to import:

package main
import (
  sp "github.com/SparkPost/gosparkpost"

Now we’ll initialize the library with connection and authentication settings, pulling our API key from the environment:

func main() {
  apiKey := os.Getenv("SPARKPOST_API_KEY")
  cfg := &sp.Config{
    BaseUrl:    "https://api.sparkpost.com",
    ApiKey:     apiKey,
    ApiVersion: 1,
  var client sp.Client
  err := client.Init(cfg)
  if err != nil {
    log.Fatalf("SparkPost client init failed: %s\n", err)

With the setup and config out of the way, what can you do today with our Go client library?

We have full support for the Transmissions, Templates, and Recipient Lists endpoints, which is enough to support most sending use cases. Templates and Recipient Lists can be pre-created, which speeds things up if you’ll be reusing the same ones a lot – they can be specified by ID. If you won’t be reusing, or if you’d simply prefer to be more dynamic, Transmissions can also be sent using an inline Template and/or Recipient List, meaning that they’re included as part of the Transmissions call.

Here’s an example of what sending a transmission looks like:

tx := &sp.Transmission{
  Recipients: []string{"someone@somedomain.com"},
  Content: sp.Content{
    HTML:    "<p>Hello world</p>",
    From:    "test@sparkpostbox.com",
    Subject: "Hello from gosparkpost",
id, _, err := client.Send(tx)
if err != nil {

Using the snippets above, you’re ready to start sending through SparkPost with Go. If you’ve been paying close attention, you’ve noticed that combining these code snippets with your API key gives you a way to send test messages. You could even build a CLI that allows you to send tests specifying the sender, recipient, and other fields as flags.

So what’s next for the Go client library?

Watch this space for more code examples and more advanced usage. We plan to use this library in several of our upcoming projects, including a sample application demonstrating how to easily parse event data out-of-band. We’re also working on support for Message Events, which lets you search through the last couple day’s worth of event data generated in your account.

What else will you build with our client libraries? We’re curious to see, so drop us a line and let us know.


Dev Survival Guide Blog Footer