/monzo-swift

Monzo API client library for Swift

Primary LanguageSwiftMIT LicenseMIT

Monzo Swift Client

Swift Linux License Travis Codebeat

A Monzo client that provides a simple Swift interface to the Monzo API. This package is targeted towards server side use on Linux; if you're looking for a Monzo client for iOS then have a look at MondoKit.

API documentation, user guides and setup information can be found at monzo.com/docs.

Installation

Note: the minimum required Swift version is DEVELOPMENT-SNAPSHOT-2016-11-08-a

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/marius-serban/monzo-swift.git"),
    ]
)

Getting started

The library is compatible with Open Swift so you can use it with any server side framework that supports this standards. It relies upon a S4 Responder to perform the HTTP calls over the network. Luckly, Vapor's Droplet and Zewo's' Client conform to this protocol for example.

Initialize your client

let myHttpClient = ...
let monzo = Monzo.Client(httpClient: myHttpClient)

Create authorization URI

Create a URI that points to the Monzo login page for your app.

let uri = Monzo.Client.authorizationUri(clientId: "aClientId", redirectUri: "http://host.com/?param=[]#fragment", nonce: "abc123")
// redirect user to URI

Authenticate

let credentials = try monzo.authenticate(withCode: " ", clientId: " ", clientSecret: " ")

or

let newCredentials = try monzo.refreshAccessToken(refreshToken: oldCredentials.refreshToken, clientId: " ", clientSecret: " ")

Ping

do {
  try monzo.ping()
} catch {
  // try again later
}

Whoami

let accessTokenInfo = try monzo.whoami(accessToken: "a_token")

Accounts

let accounts = try monzo.accounts(accessToken: "a_token")

Balance

let balance = try monzo.balance(accessToken: "a_token", accountId: "an_account_id")

Transactions

// list all transactions
let transactions = try monzo.transactions(accessToken: "a_token", accountId: "an_account_id")

// list transactions, filtered and paginated
let transactions = try monzo.transactions(accessToken: "a_token", accountId: "an_account_id", since: .transaction("txid1234"), before: referenceDate, limit: 20)

// get transaction details
let transactionDetails = try monzo.transaction(accessToken: "a_token", id: "txid1234")

Annotate transaction

try monzo.annotate(transaction: "txid1234", with metadata: ["key1": "value1", "key2": "value2"], accessToken: String)

Feed

// create simple feed item
try monzo.createFeedItem(accessToken: "a_token", accountId: "an_account", title: "Hello!", imageUrl: "http://images.domain/1")

// create fully customized feed item
try.createFeedItem(
	accessToken: "a_token",
	accountId: "an_account_id",
	title: "happy days! 🕺🏽",
	imageUrl: "http://images.domain/an-image.jpeg?param=j&other=k",
	url: "http://my.website/?param1=1&param2=2",
	body: "this is a sample body",
	backgroundColor: "#FFFFFF",
	bodyColor: "#AAAAAA",
	titleColor: "#BBBBBB"
)

Webhooks

// create
let webhook =try monzo.createWebhook(accessToken: "a_token", accountId: "account_id", url: "http://host.domain/path")

// list
let webhooks = try monzo.webhooks(accessToken: "a_token", accountId: "account_id")

// delete
try monzo.deleteWebhook(accessToken: "a_token", id: "account_id")

Support

You can create a Github issue in this repository. When stating your issue be sure to add enough details about what's causing the problem and reproduction steps.

Also, you can get in touch with me on Twitter.

License

This project is released under the MIT license. See LICENSE for details.