# Go

> Send your first email from Go using net/http against the SendPing API.

This guide sends email by POSTing JSON to `https://www.sendping.co/api/emails` using only the standard-library `net/http` and `encoding/json`, so there are no third-party dependencies. Want the typed client instead? [Send emails with Go](https://www.sendping.co/docs/integrations/go) installs the official module.

## Prerequisites

- A SendPing [API key](https://www.sendping.co/docs/api-keys/overview).
- A [verified domain](https://www.sendping.co/docs/domains/managing) to send from.
- Go 1.22 or newer if you use the official module (that is the floor in its `go.mod`). The standard-library example below builds on Go 1.18 and up (it uses the `any` alias).

## 1. Set your API key

**.env**

```sh
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 2. Send email using HTML

The easiest way to send an email is with the `html` field.

**main.go**

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

func main() {
	payload, _ := json.Marshal(map[string]any{
		"from":    "Acme <onboarding@yourdomain.com>",
		"to":      []string{"delivered@test.sendping.co"},
		"subject": "Hello from Go",
		"html":    "<strong>hello world</strong>",
	})

	req, _ := http.NewRequest("POST", "https://www.sendping.co/api/emails", bytes.NewReader(payload))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("SENDPING_API_KEY"))
	req.Header.Set("Content-Type", "application/json")
	// Required: the API answers a missing User-Agent with 403 validation_error.
	req.Header.Set("User-Agent", "acme-app/1.0")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	defer res.Body.Close()

	var out struct {
		ID string `json:"id"`
	}
	json.NewDecoder(res.Body).Decode(&out)
	fmt.Println(out.ID)
}
```

> **Note:** Drop this into a Chi or Gin handler to send from a web request. See [Send an email](https://www.sendping.co/docs/api/emails-send) for `cc`, `bcc`, `reply_to`, and more.
