{"id":18990771,"url":"https://github.com/mailslurp/mailslurp-client-swift","last_synced_at":"2025-04-22T11:29:32.058Z","repository":{"id":41473625,"uuid":"310586458","full_name":"mailslurp/mailslurp-client-swift","owner":"mailslurp","description":"Official MailSlurp Email API Client for Swift ","archived":false,"fork":false,"pushed_at":"2024-06-03T06:17:33.000Z","size":2681,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-15T08:04:10.947Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mailslurp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":"SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-06T12:04:30.000Z","updated_at":"2024-09-18T17:15:13.000Z","dependencies_parsed_at":"2024-01-13T12:20:28.984Z","dependency_job_id":"fbd7ed45-f052-4054-89ba-8eaf88712da9","html_url":"https://github.com/mailslurp/mailslurp-client-swift","commit_stats":{"total_commits":1,"total_committers":1,"mean_commits":1.0,"dds":0.0,"last_synced_commit":"15865a4dc944f6d6e1a1b9b3d25dae574cb37bae"},"previous_names":[],"tags_count":389,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailslurp%2Fmailslurp-client-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailslurp%2Fmailslurp-client-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailslurp%2Fmailslurp-client-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mailslurp%2Fmailslurp-client-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mailslurp","download_url":"https://codeload.github.com/mailslurp/mailslurp-client-swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250231558,"owners_count":21396477,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T17:11:19.105Z","updated_at":"2025-04-22T11:29:32.032Z","avatar_url":"https://github.com/mailslurp.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MailSlurp Swift Email Client\n\nSend and receive emails in Swift and Objective C. Use the MailSlurp Swift client for managing email accounts in iOS, MacOS, TVOS, and more. Built with OpenAPI and PromiseKit.\n\n## Quick links\n\n- [API documentation](https://docs.mailslurp.com/api/)\n- [Swift getting started guide](https://www.mailslurp.com/examples/)\n- [SwiftPackageIndex](https://swiftpackageindex.com/mailslurp/mailslurp-client-swift)\n- [Github Source](https://github.com/mailslurp/mailslurp-client-swift/)\n- [Method Documentation](https://github.com/mailslurp/mailslurp-client-swift/tree/master/docs/)\n- [iOS XCode setup example](https://www.mailslurp.com/examples/ios-xcode-send-and-read-email-example/)\n- [Support](https://www.mailslurp.com/support/)\n\n## Install MailSlurp package\nThe Swift package is hosted on the [Swift Package Index](https://swiftpackageindex.com/mailslurp/mailslurp-client-swift) and [GitHub](https://github.com/mailslurp/mailslurp-client-swift). Create a new swift project using:\n\n```bash\nswift package init --type=executable\n```\n\nEdit `Packages.swift` and add `https://github.com/mailslurp/mailslurp-client-swift` as a package **and** a target dependency.\n\n```swift\n// swift-tools-version:5.5\n\nimport PackageDescription\n\nlet package = Package(\n        name: \"your-project\",\n        products: [\n        ],\n        dependencies: [\n            .package(url: \"https://github.com/mailslurp/mailslurp-client-swift\", from: \"15.12.5\"),\n        ],\n        targets: [\n            .testTarget(\n                    name: \"your-test-project\",\n                    dependencies: [\n                        .product(name: \"mailslurp\", package: \"mailslurp-client-swift\")\n                    ]),\n        ]\n)\n```\n\nMailSlurp client is built with PromiseKit (async/await for swift) so you may also want to include [AwaitKit](https://github.com/yannickl/AwaitKit) in your swift dependencies for awaiting Promise results.\n\n## Setup API Key\n\nMailSlurp is free to use but requires an API Key. [Sign up](https://app.mailslurp.com) and create an API Key using the [MailSlurp dashboard](https://app.mailslurp.com).\n\nWhen you make requests use an API controller's `withRequestBuilder` method call the `addHeader(\"x-api-key\", \"your-api-key\")` method to add authentication to your requests. For example when using the InboxController methods:\n\n```swift\nlet promise = InboxControllerAPI\n    .createInboxWithDefaultsWithRequestBuilder()\n    .addHeader(name: \"x-api-key\", value: apiKey)\n    .execute()\n```\n\nThe full test example is as follows:\n\n```swift\nlet apiKey = ProcessInfo.processInfo.environment[\"API_KEY\"] ?? \"\"\nInboxControllerAPI.createInboxWithDefaultsWithRequestBuilder()\n    // use addHeader to add your api key to requests\n    .addHeader(name: \"x-api-key\", value: apiKey)\n    .execute()\n    // uses PromiseKit for response handling\n    .done { response in\n        XCTAssertTrue(response.body?.emailAddress?.contains(\"@mailslurp\") ?? false)\n        XCTAssertTrue(response.statusCode == 201)\n        expectation.fulfill()\n    }.catch { errorType in \n        // handle error, cast to mailslurp.ErrorResponse using guard to access .error(statusCode, data, _, _)\n    }\n```\n\n## API structure\n\nThe Swift client mimics the [controllers and methods of the REST API](https://docs.mailslurp.com/api/). For controller and method documentation please see the [code hosted on GitHub](https://github.com/mailslurp/mailslurp-client-swift) or read the [getting started guide](https://www.mailslurp.com/guides/getting-started/).\n\n## Platform compatibility\n\nMailSlurp Swift client is built to target Swift 5.x and XCode 12.x.\n\n| Platform | Minimum Version |\n|----------|-----------------|\n| iOS      | v9              |\n| MacOS    | V10.11          |\n| TVOS     | v9              |\n| WatchOS  | v3              |\n\n## Example usage\n\nMailSlurp uses PromiseKit. To test using XCTest and MailSlurp use the example below:\n\n```swift\nimport XCTest\nimport class Foundation.Bundle\nimport mailslurp\n\nfinal class exampleTests: XCTestCase {\n    func testExample() throws {\n        let expectation = self.expectation(description: \"Can create inbox\")\n        let apiKey = ProcessInfo.processInfo.environment[\"API_KEY\"] ?? \"\"\n        InboxControllerAPI.createInboxWithDefaultsWithRequestBuilder()\n            .addHeader(name: \"x-api-key\", value: apiKey)\n            .execute()\n            .done { response in\n                XCTAssertTrue(response.statusCode == 201)\n                XCTAssertTrue(response.body?.emailAddress?.contains(\"@mailslurp\") ?? false)\n                expectation.fulfill()\n             }.catch { errorType in\n                XCTAssertFalse(true, \"Should not throw exception\")\n             }\n        waitForExpectations(timeout: 5, handler: nil)\n\n    }\n\n    /// Returns path to the built products directory.\n    var productsDirectory: URL {\n      #if os(macOS)\n        for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(\".xctest\") {\n            return bundle.bundleURL.deletingLastPathComponent()\n        }\n        fatalError(\"couldn't find the products directory\")\n      #else\n        return Bundle.main.bundleURL\n      #endif\n    }\n}\n```\n\nTo run the test execute:\n\n```bash\nAPI_KEY=your-api-key swift test\n```\n\n### Handling exceptions\n\nUse a guard to cast errors to a `mailslurp.ErrorResponse` to access the status code and error message of a request.\n\n```swift\nCommonActionsControllerAPI.sendEmailSimpleWithRequestBuilder(emailOptions: sendOptions)\n  .addHeader(name: \"x-api-key\", value: apiKey)\n  .execute()\n  .done { response in\n    // handle success\n  }\n  .catch(policy: .allErrors) { err in\n    // handle error\n    guard let e = err as? mailslurp.ErrorResponse else {\n        error = err.localizedDescription\n        return\n    }\n    // pattern match the error to access status code and data\n    // MailSlurp returns 4xx errors when invalid parameters or\n    // unsatisfiable request. See the message and status code\n    switch e {\n    case .error(let statusCode, let data, _, _):\n        let msg = String(decoding: data!, as: UTF8.self)\n        error = \"\\(statusCode) Bad request: \\(msg)\"\n    }\n  }\n```\n\n### Awaiting promises\n\nPromiseKit wraps MailSlurp request results in a Promise. Use `.done { response in }` and `.catch { err in }` methods to handle responses and exceptions. For easier access use AwaitKit style await functions.\n\n```swift\nlet inbox = try! await(\n    InboxControllerAPI.createInboxWithDefaultsWithRequestBuilder()\n        .addHeader(name: \"x-api-key\", value: apiKey)\n        .execute()\n)\n// then access inbox.emailAddress, inbox.id etc\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmailslurp%2Fmailslurp-client-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmailslurp%2Fmailslurp-client-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmailslurp%2Fmailslurp-client-swift/lists"}