Skip to content

TelemetryDeck/brevo-kit

Repository files navigation

brevo-kit

Swift Vapor SDK for Brevo

Generated from the Brevo API OpenAPI specification using Swift OpenAPI Generator.

See the Brevo API docs for more information on how to use the API.

Installation

Add the package dependency in your Package.swift:

.package(url: "https://github.com/telemetryDeck/brevo-kit", from: "1.0.0"),

Next, in your target, add BrevoKit to your dependencies:

.target(name: "MyTarget", dependencies: [
    .product(name: "BrevoKit", package: "brevo-kit"),
]),

Setup

Get an API key from your Brevo account here: https://app.brevo.com/settings/keys/api

Init with API Key:

let brevo: Brevo = try Brevo(apiKey: "xkeysib-...", sandbox: false)

TODO: Setup with Vapor.

Usage

Send an email with defined content:

try await brevo.email.send(
    from: .init(email: "root@eruditorium.org", name: "Enoch Root"),
    to: [.init(email: "hello@example.com")],
    subject: "Hello World",
    htmlContent: nil,
    textContent: "Hello World!"
)

Send an email with a template:

try await brevo.email.send(
    to: [.init(email: "hello@example.com")],
    templateID: 1,
    parameters: ["name": "Daniel"],
    tags: ["test", "brevo-kit"]
)

Tracing

BrevoKit is instrumented with swift-distributed-tracing. Every API call made through brevo.email, brevo.contacts, and brevo.events is wrapped in a client span (e.g. brevo.email.send, brevo.contacts.get, brevo.events.create), and thrown errors are recorded on the span automatically.

This is zero-overhead unless you opt in: until your application bootstraps a tracer, all spans resolve to the no-op tracer and nothing is emitted. To collect traces, bootstrap a Tracer once at startup, for example using swift-otel:

import Tracing
import OTel

let observability = try OTel.bootstrap()
// ... your app runs; BrevoKit spans now flow to your OTLP backend

Spans carry non-sensitive attributes only (recipient counts, template IDs, identifier type) — recipient email addresses and contact identifiers are intentionally kept out of trace data.

Development

To update the generated code from the OpenAPI specification, you can use the generate-code-from-openapi plugin provided by Apple, take these steps:

  1. Download a new version of the Brevo API OpenAPI specification, place it into openapi.yml
  2. Uncomment the swift-openapi-generator package dependency in Package.swift
  3. Run swift package plugin generate-code-from-openapi --target BrevoKit

If an error occurs, try deleting the .build directory.