SendGrid
Send email through SendGrid with the @effect-kit/emails/sendgrid Layer.
The @effect-kit/emails/sendgrid subpath sends messages through SendGrid and provides the Email service.
Installation
npm install @effect-kit/emails effect @sendgrid/mail@8.1.6The SDK is a pinned optional peer. It is needed only when you import this subpath.
Setup
SendGrid.layerConfig() reads these variables:
| Variable | Required | Value |
|---|---|---|
SENDGRID_API_KEY | yes | API key |
Example (Sending a Message With SendGrid)
import { , , } from "effect";
import { } from "@effect-kit/emails";
import * as from "@effect-kit/emails/sendgrid";
const = .().(
.(.(.())),
);
const = .(function* () {
const = yield* ;
return yield* .({
: "hello@example.com",
: "ada@example.com",
: "Welcome",
: "Your account is ready.",
});
});
.(.(.()));Use a from address your SendGrid account is allowed to send from.
Options
SendGrid.layer takes these options. layerConfig builds the same object from a Config.
Prop
Type
Capabilities
SendGrid reports this in email.capabilities:
| Text | HTML | Both | Sender name | Cc | Bcc | Reply-to | Headers | Attachments | Inline images | Idempotency |
|---|---|---|---|---|---|---|---|---|---|---|
| yes | yes | yes | yes | yes | yes | one | yes | yes | yes | no |
These fail with UnsupportedFeatureError and acceptance: "notSubmitted" before any request:
- More than one
replyToaddress (featuremultipleReplyTo). - An
idempotencyKey(featureidempotency).
Idempotency
Not supported. A key fails with UnsupportedFeatureError before sending.
Provider notes
- SendGrid takes one reply-to address. A second
replyToaddress fails before sending with featuremultipleReplyTo. - A send is accepted only on an HTTP 202 response. Any other success status fails with
UnexpectedResponseError. The receipt's reference is thex-message-idresponse header, andreferencesis empty when the header is missing.
Using your own client
layerClient takes any object with send(message), which the @sendgrid/mail mail service has. options is optional.
Example (Borrowing a SendGrid Client)
import sgMail from "@sendgrid/mail";
import * as SendGrid from "@effect-kit/emails/sendgrid";
sgMail.setApiKey("SG.fake_key");
const EmailLive = SendGrid.layerClient(sgMail);You own a borrowed client. The Layer never closes it, and the client keeps its own retry, timeout and logging policy.
Errors
ProviderConfigurationError carries provider: "sendgrid" and a field naming the bad option. Layer construction fails with it, and layerClient fails with it for the client options.
field | Raised when |
|---|---|
timeout | The timeout is not finite and positive. |
token | The API key is not printable ASCII without spaces. |
| SendGrid failure | Reason |
|---|---|
SDK error with a numeric code | Mapped from that code as an HTTP status |
Any other HTTP status uses the shared mapping: 401 is AuthenticationError, 403 PermissionDeniedError, 429 RateLimitedError, 400 and 422 InvalidMessageError, 5xx ProviderUnavailableError, and anything else ProviderError. acceptance is rejected for a 4xx status other than 409 and unknown otherwise. See Errors. A network failure with no status is TransportError, and ETIMEDOUT or ECONNABORTED is TimeoutError, both with acceptance: "unknown".