Skip to content

IndiePitcher/indiepitcher-swift

Repository files navigation

Horizontal logo Brand purple

IndiePitcher Server-side Swift SDK

Official IndiePitcher SDK for Swift language.

Provides a type safe layer on top IndiePitcher's public REST API.

SDK documentation can be found here.

Instalation

The SDK is designed to work with any framework built on top of Swift Nio - Vapor, Hummingbird, or AWS Lamda is supported.

  1. Add the dependency to your Package.swift file
.package(url: "https://github.com/IndiePitcher/indiepitcher-swift.git", from: "1.0.0"),
  1. Add IndiePitcher to appropriate target(s) of your project
.product(name: "IndiePitcherSwift", package: "indiepitcher-swift")

You can also use the CLI instead

swift package add-dependency https://github.com/IndiePitcher/indiepitcher-swift.git --from 1.0.0
swift package add-target-dependency IndiePitcherSwift --package indiepitcher-swift MyTarget

Using the SDK:

  • First, you need to get an API key. Go to the IndiePitcher dashboard, create a project, and generate a public API key.
  • Add the key to your .env file. Following examples will assume that you've added the key under IP_SECRET_API_KEY key.

Vapor 4

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: "[email protected]", subject: "Hello from Vapor!", body: emailBody,
                bodyFormat: .markdown))

    return "ok"
}

See the full example repository.


Hummingbird 2

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
}

See the full example repository.


AWS Lambda

This is how you can send an email from within an AWS Lambda function. See the full example repository.

@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: "[email protected]", subject: "Hello from AWS Lambda!", body: emailBody,
                bodyFormat: .markdown))

        return "Email sent!"
    }
}

About

Official IndiePitcher SDK for server-side Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages