Introduction

Server-side Swift is transforming the landscape of web development by bringing the speed, safety, and modern syntax of Swift to backend programming. The backend of IndiePitcher itself is built with Swift and we provide an official SDK to make it easy to integrate IndiePitcher with your server-side Swift apps.

Official Swift SDK

Integrate IndiePitcher with your server-side Swift apps using our official SDK.

Though it is technically possible, we don’t recommend using the SDK from client iOS/macOS apps. You’re risking your secret API key being exposed. The risk is lower than with client-side JavaScript, but it’s still there. You can easily set up an AWS lambda function to send an email on behalf of your app.

Integrations

Vapor

Create a new file, something like Application+IndiePitcher.swift and paste in following code

import Vapor
import IndiePitcherSwift

extension Request {
    var indiePitcher: IndiePitcher {
        guard let apiKey = Environment.get("IP_V2_SECRET_API_KEY") else {
            fatalError("IP_V2_SECRET_API_KEY env key missing")
        }

        return .init(client: application.http.client.shared, apiKey: apiKey)
    }
}

extension Application {
    var indiePitcher: IndiePitcher {
        guard let apiKey = Environment.get("IP_V2_SECRET_API_KEY") else {
            fatalError("IP_V2_SECRET_API_KEY env key missing")
        }

        return .init(client: http.client.shared, apiKey: apiKey)
    }
}

This will give you easy access to the SDK methods using application and request.

app.get { req async in

    let emailBody = """
    This is a sample body that supports **markdown**. HTML is also supported.
    """
 
    try await indiePitcher.sendEmail(
            data: .init(
                to: "petr@indiepitcher.com", subject: "Hello from Vapor!", body: emailBody,
                bodyFormat: .markdown))

    return "ok"
}

Example Repository

Explore the example repository to see how to integrame IndiePitcher with Vapor.

Hummingbird

public func buildApplication(_ arguments: some AppArguments) async throws
    -> some ApplicationProtocol
{
    let environment = try await Environment().merging(with: .dotEnv())

    let logger = {
        var logger = Logger(label: "HummingbirdExample")
        logger.logLevel =
            arguments.logLevel ?? environment.get("LOG_LEVEL").flatMap {
                Logger.Level(rawValue: $0)
            } ?? .info
        return logger
    }()

    guard let apiKey = environment.get("INDIEPITCHER_SECRET_KEY") else {
        preconditionFailure("Requires \"INDIEPITCHER_SECRET_KEY\" environment variable")
    }

    let indiePitcher = IndiePitcher(apiKey: apiKey)

    let router = buildRouter(indiePitcher: indiePitcher)
    let app = Application(
        router: router,
        configuration: .init(
            address: .hostname(arguments.hostname, port: arguments.port),
            serverName: "HummingbirdExample"
        ),
        logger: logger
    )
    return app
}

Example Repository

Explore the example repository to see how to integrame IndiePitcher with Hummingbird.

AWS Lambda

We recommend using AWSLambdaRuntime package to build your Lambda functions in Swift.

// Import the module
import AWSLambdaRuntime
import AsyncHTTPClient
import IndiePitcherSwift

@main
struct MyLambda: SimpleLambdaHandler {

    private let indiePitcherApiKey = "sc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

    func handle(_ event: String, context: LambdaContext) async throws -> String {

        let indiePitcher = IndiePitcher(apiKey: indiePitcherApiKey)

        let emailBody = """
            This is an email sent from a **AWS Lambda function**!
            """

        try await indiePitcher.sendEmail(
            data: .init(
                to: "petr@indiepitcher.com", subject: "Hello from AWS Lambda!", body: emailBody,
                bodyFormat: .markdown))

        return "Email sent!"
    }
}

Example Repository

Explore the example repository to see how to set up a Swift AWS Lambda function that sends emails with IndiePitcher.