ApiKit is a Swift library that makes it easy to integrate with any external REST APIs.
ApiKit has an ApiClient
protocol that can fetch any URLRequest
and decode the data to any Decodable
type. It's implemented by URLSession
so you can either use URLSession.shared
or create your own custom service.
ApiKit has an ApiEnvironment
and ApiRoute
model that can be used to model the available environments and routes for any REST API, such as the base URL of a certain API environment, the URL of a certain route, which parameters and headers to send etc.
Any ApiClient
can then fetch any ApiRoute
from any ApiEnvironment
and automatically have the result decoded to any Decodable
type.
ApiKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/ApiKit.git
If you prefer to not have external dependencies, you can also just copy the source code into your app.
ApiKit supports iOS 13
, macOS 11
, tvOS 13
and watchOS 6
.
Implementing API integrations with ApiKit is very easy. You can either fetch raw URLRequest
s and handle the raw data, or create custom ApiEnvironment
and ApiRoute
types to model various APIs.
For instance, with a TheMovieDb-specific environment:
enum TheMovieDbEnvironment: ApiEnvironment {
case production(apiKey: String)
public var url: String {
switch self {
case .production: return "https://api.themoviedb.org/3"
}
}
public var headers: [String: String]? { nil }
public var queryParams: [String: String]? {
switch self {
case .production(let key): return ["api_key": key]
}
}
}
and a TheMovieDb-specific route:
enum Route: ApiRoute {
case movie(id: Int)
public var path: String {
switch self {
case .movie(let id): return "movie/\(id)"
}
}
public var queryParams: [String: String]? {
switch self {
case .movie: return nil
}
}
public var httpMethod: HttpMethod { .get }
public var headers: [String: String]? { nil }
public var formParams: [String: String]? { nil }
public var postData: Data? { nil }
}
we could easily fetch movies like this:
let client = URLSession.shared
let environment = TheMovieDb.Environment.production("API_KEY")
let route = TheMovieDb.Route.movie(id: 123)
let movie: TheMovieDb.Movie = try await client.fetchItem(at: route, in: environment)
For more information, please see the online documentation and getting started guide guide.
The online documentation contains more information, code examples, etc., and makes it easy to overview the various parts of the library.
The demo app lets you explore the library on iOS and macOS. To try it out, just open and run the Demo
project.
You can sponsor this project on GitHub Sponsors or get in touch for paid support.
Feel free to reach out if you have questions or if you want to contribute in any way:
- Website: danielsaidi.com
- Mastodon: @danielsaidi@mastodon.social
- Twitter: @danielsaidi
- E-mail: daniel.saidi@gmail.com
ApiKit is available under the MIT license. See the LICENSE file for more info.