sendgrid-go
repository·main·Indexed 21 days ago
https://github.com/sendgrid/sendgrid-goA Go client library for interacting with the Twilio SendGrid Web API v3. It provides high-level helpers for common tasks such as sending emails via the mail helper, managing SecureWebhook features with the eventwebhook helper, and processing Inbound Parse webhook data with the inbound helper, as well as low-level access for direct API requests.
What's inside sendgrid-go
- For developers integrating SendGrid with Twilio services, there are specific guides available for Twilio setup and for sending emails using the Twilio Email pilot program.
Explore SendGrid Email Use Cases
mainThesendgrid-gorepository provides documentation and examples for various common email implementation patterns. These include managing transactional templates, handling personalizations, using custom arguments, and managing attachments. You can find detailed guides for each of these specific workflows in theuse-cases/directory.Use the mail helper to build Mail objects
mainThe
mailhelper package provides utilities to quickly and easily constructMailobjects required for sending emails via the Twilio SendGrid API. It simplifies the process of setting up the complex nested structures used in the SendGrid Mail Send API.To use this helper, ensure you have the
restdependency installed and yourSENDGRID_API_KEYenvironment variable configured.Use the Event Webhook helper to manage SecureWebhook features
mainThe
eventwebhookhelper provides functionality to enable or disable the SecureWebhook feature and retrieve the public key from Twilio SendGrid. This is useful for verifying the authenticity of event webhooks sent by SendGrid.Dependencies:
github.com/sendgrid/rest
Manage Twilio SendGrid API Key via environment variables
mainIt is recommended to use environment variables to store your
SENDGRID_API_KEYrather than hardcoding it.When using
os.Getenv("SENDGRID_API_KEY"), the code is looking for the name of the environment variable. If you hardcode the string, you are providing the actual API key value directly.Version management and SemVer
mainThe library follows Semantic Versioning (MAJOR.MINOR.PATCH). It is highly recommended to pin or vendor the specific version you are using in your code to prevent breaking changes from automatic updates, especially during MAJOR version releases.Manage IP Access Settings
mainIP Access Management allows you to control which IP addresses can access your account via the UI or API. You can whitelist specific IPs, retrieve activity logs, or manage existing whitelist rules.
Warning: It is possible to remove your own IP address from the whitelist, which will prevent you from accessing your account.
Manage subuser link branding associations
mainLink branding can be associated with subusers from a parent account. This allows subusers to send mail using the parent's branded links.
- Retrieve associated branding: Use
GET /v3/whitelabel/links/subuserwith ausernamequery parameter. - Associate branding: Use
POST /v3/whitelabel/links/{link_id}/subuserwith a JSON body containing theusername. - Disassociate branding: Use
DELETE /v3/whitelabel/links/subuserwith ausernamequery parameter.
// Associate a link branding with a subuser request := sendgrid.GetRequest(apiKey, "/v3/whitelabel/links/{link_id}/subuser", host) request.Method = "POST" request.Body = []byte(` {\n "username": "jane@example.com"\n}`) response, err := sendgrid.API(request)- Retrieve associated branding: Use
Understand the ParsedEmail data structure
mainThe
ParsedEmailstruct contains several fields for accessing different parts of the inbound email. Use the following guide to choose the correct field for your needs:Field Description Envelope.To/Envelope.FromExact email addresses. These are safe to use without further parsing. ParsedValuesPre-parsed fields provided by SendGrid. Recommended over Headersfor consistency.TextBodyThe email body pre-parsed into a plain text string separated by \n.ParsedAttachmentsPopulated only when using ParseWithAttachments(). ProvidesFile(the content),Size,Filename, andContentType.BodyPopulated only if 'Raw' is enabled in the SendGrid Dashboard. Contains the raw HTML body. AttachmentsDEPRECATED. Use ParsedAttachmentsinstead.HeadersDEPRECATED. Use ParsedValuesinstead. Raw headers may be inconsistent across email clients (e.g., Outlook.com).Note on Attachments: To access attachment data easily without requiring the 'Raw' setting in your SendGrid Dashboard, use
ParsedAttachmentsvia theParseWithAttachments()function.Make API calls on behalf of a subuser
mainThe
on-behalf-offunctionality allows you to perform actions for a specific subuser using your parent account credentials. This is useful for automation without changing authentication keys.Use
sendgrid.GetRequestSubuserto implement this.Note: The
v3/mail/sendendpoint does not support theon-behalf-ofheader.request := sendgrid.GetRequestSubuser( os.Getenv("SENDGRID_API_KEY"), "/v3/tracking_settings/subscription", "https://api.sendgrid.com", "SUBUSER_USERNAME", )Manage Alerts
mainAlerts allow you to receive notifications regarding your email usage or statistics. There are two main types:
- Usage alerts: Set a threshold to trigger a notification.
- Stats notifications: Set a frequency (e.g.,
daily,weekly,monthly) to receive email statistics reports.
Manage IP Pools
mainIP Pools allow you to group dedicated Twilio SendGrid IP addresses together to maintain separate reputations for different types of traffic (e.g., transactional vs. marketing). Each user can create up to 10 IP pools. IP pools can only be used with authenticated IP addresses. If no pool is specified for an email, SendGrid will use any available IP, including those in pools.
Available operations:
- Create a pool:
POST /v3/ips/pools - Retrieve all pools:
GET /v3/ips/pools - Update a pool name:
PUT /v3/ips/pools/{pool_name} - Retrieve all IPs in a pool:
GET /v3/ips/pools/{pool_name} - Delete a pool:
DELETE /v3/ips/pools/{pool_name}
// Example: Create an IP pool named 'marketing' request := sendgrid.GetRequest(apiKey, "/v3/ips/pools", host) request.Method = "POST" request.Body = []byte(` { "name": "marketing" }`) response, err := sendgrid.API(request)- Create a pool: