Skip to contentSkip to navigationSkip to topbar
On this page

v3 API Go Code Example


(information)

Info

We recommend using SendGrid Go, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

Do you have an API Key(link takes you to an external page) yet? If not, go get one. You're going to need it to integrate!


Using SendGrid's Go Library

using-sendgrids-go-library page anchor
1
// using SendGrid's Go Library
2
// https://github.com/sendgrid/sendgrid-go
3
package main
4
5
import (
6
"fmt"
7
"log"
8
"os"
9
10
"github.com/sendgrid/sendgrid-go"
11
"github.com/sendgrid/sendgrid-go/helpers/mail"
12
)
13
14
func main() {
15
from := mail.NewEmail("Example User", "test@example.com")
16
subject := "Sending with SendGrid is Fun"
17
to := mail.NewEmail("Example User", "test@example.com")
18
plainTextContent := "and easy to do anywhere, even with Go"
19
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
20
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
21
client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
22
response, err := client.Send(message)
23
if err != nil {
24
log.Println(err)
25
} else {
26
fmt.Println(response.StatusCode)
27
fmt.Println(response.Body)
28
fmt.Println(response.Headers)
29
}
30
}