Swift SDK
Send email, manage domains, contacts, templates, and webhooks from Swift on macOS and iOS with the official TaifaMail package.
The official Swift SDK is published as the TaifaMail Swift package. Source lives in the taifa-mail-swift repository.
The package is built on async/await and Foundation's URLSession, with no third-party dependencies. It supports macOS 12+ and iOS 15+.
Installation
Add the package to the dependencies in your Package.swift:
Then add TaifaMail to your target's dependencies:
In Xcode, you can instead use File -> Add Packages, paste the repository URL https://github.com/GovConnectKenya/taifa-mail-swift.git, and add the TaifaMail library product to your target.
Client setup
Create an API key in the dashboard under Settings -> API Keys. Keys start with tfm_k_. Construct the client with that key:
The convenience initializer accepts these parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey | String | (required) | Your API key. Starts with tfm_k_. |
baseURL | String | https://govconnect.ke | Override the API base URL. |
maxRetries | Int | 3 | Total attempts on 429 / 5xx, including the first. |
timeout | TimeInterval | 30 | Per-request timeout in seconds. |
session | URLSession | .shared | Inject a custom URLSession (for testing). |
The client exposes one handle per resource group: taifamail.emails, taifamail.domains, taifamail.contacts, taifamail.suppressions, taifamail.templates, and taifamail.webhooks. Every method is async throws.
Send an email
emails.send(_:) queues a single message and returns its id and initial status. Address fields accept a bare string literal, which the SDK treats as an Address(email:).
The response is a SendEmailResponse with id, status, messageId, and rejectionReason.
A few conveniences worth knowing:
- The SDK exposes a clean
fromfield, then maps it to the wire fieldfrom_for you. You never writefrom_. Addressconforms toExpressibleByStringLiteral, so"customer@example.com"is sugar forAddress(email: "customer@example.com"). UseAddress(email:name:)when you want a display name.- Provide
html,text, or both. sendAtaccepts an ISO 8601 string and schedules the message for later (Starter plan and up).
Emails
Scheduled emails:
Saved searches (named filter sets stored per user) are loosely typed as JSONObject:
Domains
The niche domain endpoints (DNS provider hints, BIMI, and Domain Connect) are not covered by this version of the SDK. Call them directly over HTTP if you need them.
Contacts
Subscriber lists, their contacts, CSV imports, and templated bulk sends.
uploadCsv takes the raw file bytes (Data) and a filename, sent as a single multipart field named file:
bulkSend mails a templated message to every contact in a list. Subject, html, and text may use {{email}}, {{name}}, and {{metadata_key}} placeholders. The contact_list_id field is injected for you to match the list id:
Suppressions
The do-not-send list. list returns a paginated Page envelope (items, total, page, limit).
Templates
Reusable email templates. html maps to the wire field html_body and text to text_body for you.
A template's variables are derived server-side from the {{name}} placeholders in its bodies, so you do not pass them when creating or updating. Templates require the Starter plan or above.
Webhooks
Error handling
Every non-2xx response, and any transport failure that survives all retries, throws an TaifaMailError. Inspect status and code to branch on specific failures. A status of 0 means a transport or network failure where no HTTP response was received.
TaifaMailError conforms to LocalizedError and CustomStringConvertible, so its errorDescription is the message and printing it gives a status / code / message summary.
Configuration
- Retries. Requests that return
429or5xxare retried with exponential backoff, honouring theRetry-Afterheader when present. Control the total attempt count (including the first) withmaxRetries(default3). Multipart uploads are not retried because they are not idempotent. - Timeout. Each request times out after
timeoutseconds (default30). A timeout is treated as a transport failure and is retried while attempts remain. - Custom base URL. Point the client at a staging or self-hosted host with
baseURL. A trailing slash is trimmed automatically. - Custom session. Pass a
URLSessionto share configuration or to inject a mock transport in tests.
Next steps
- SDKs overview for the full list of official SDKs.
- Emails API reference for the underlying HTTP endpoints, fields, and limits.