Quick setup examples

Encore.go

Send your first email from an Encore Go API endpoint using net/http against the MailBlastr API.

Encore is an open-source Go framework that provisions infrastructure directly from your application code. The mailblastr SDK is for Node.js — Go users send email by POSTing JSON to https://api.mailblastr.com/emails with the standard-library net/http, reading your key from Encore's secrets management.

Prerequisites

1. Create an Encore app

encore app create --lang=go my-app

2. Set your API key

Encore has built-in secrets management. Store your MailBlastr API key as a secret — no .env files needed.

encore secret set --type dev,local,pr,production MailBlastrAPIKey

3. Send email from an API endpoint

Create an email service directory and define your endpoint. Encore injects the secret through an unexported secrets struct; the handler POSTs the email body to the MailBlastr API with net/http.

email/email.go
package email

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

var secrets struct {
	MailBlastrAPIKey string
}

type SendRequest struct {
	To      string `json:"to"`
	Subject string `json:"subject"`
	HTML    string `json:"html"`
}

type SendResponse struct {
	ID string `json:"id"`
}

//encore:api public method=POST path=/email/send
func Send(ctx context.Context, req *SendRequest) (*SendResponse, error) {
	payload, _ := json.Marshal(map[string]any{
		"from":    "Acme <onboarding@yourdomain.com>",
		"to":      []string{req.To},
		"subject": req.Subject,
		"html":    req.HTML,
	})

	httpReq, err := http.NewRequestWithContext(
		ctx, "POST", "https://api.mailblastr.com/emails", bytes.NewReader(payload),
	)
	if err != nil {
		return nil, err
	}
	httpReq.Header.Set("Authorization", "Bearer "+secrets.MailBlastrAPIKey)
	httpReq.Header.Set("Content-Type", "application/json")

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

	if res.StatusCode >= 300 {
		return nil, fmt.Errorf("failed to send email: status %d", res.StatusCode)
	}

	var out SendResponse
	if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
		return nil, err
	}

	return &out, nil
}

4. Run the app

encore run

Your API is running at http://localhost:4000. Send a test email:

curl -X POST http://localhost:4000/email/send \
  -H "Content-Type: application/json" \
  -d '{"to":"delivered@example.com","subject":"Hello World","html":"<strong>It works!</strong>"}'
See Send an email for the complete request body — cc, bcc, reply_to, attachments, tags, and scheduled_at.