> ## Documentation Index
> Fetch the complete documentation index at: https://docs.indiepitcher.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift

> Use IndiePitcher with your server-side Swift apps

## 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.

<Card icon="github" title="Official Swift SDK" href="https://github.com/IndiePitcher/indiepitcher-swift">
  Integrate IndiePitcher with your server-side Swift apps using our official SDK.
</Card>

<Warning>
  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.
</Warning>

## Integrations

### Vapor

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

```swift theme={null}
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`.

```swift theme={null}
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"
}
```

<Card icon="github" title="Example Repository" href="https://github.com/IndiePitcher/VaporExample">
  Explore the example repository to see how to integrame IndiePitcher with Vapor.
</Card>

### Hummingbird

```swift theme={null}
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
}
```

<Card icon="github" title="Example Repository" href="https://github.com/IndiePitcher/HummingbirdExample">
  Explore the example repository to see how to integrame IndiePitcher with Hummingbird.
</Card>

### AWS Lambda

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

```swift theme={null}
// 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!"
    }
}
```

<Card icon="github" title="Example Repository" href="https://github.com/IndiePitcher/IndiePitcherLambdaSwiftExample">
  Explore the example repository to see how to set up a Swift AWS Lambda function that sends emails with IndiePitcher.
</Card>
