# Encore.go

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

[Encore](https://encore.dev) 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

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.
- Install [Encore](https://encore.dev/docs/go/install) (`brew install encoredev/tap/encore`).

## 1. Create an Encore app

```bash
encore app create --lang=go my-app
```

## 2. Set your API key

Encore has built-in [secrets management](https://encore.dev/docs/go/primitives/secrets). Store your MailBlastr API key as a secret — no `.env` files needed.

```bash
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**

```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

```bash
encore run
```

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

```bash
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>"}'
```

> **Note:** See [Send an email](https://www.mailblastr.com/docs/api/emails-send) for the complete request body — `cc`, `bcc`, `reply_to`, `attachments`, `tags`, and `scheduled_at`.
