Monzo 0.12.1
dotnet add package Monzo --version 0.12.1
NuGet\Install-Package Monzo -Version 0.12.1
<PackageReference Include="Monzo" Version="0.12.1" />
paket add Monzo --version 0.12.1
#r "nuget: Monzo, 0.12.1"
// Install Monzo as a Cake Addin
#addin nuget:?package=Monzo&version=0.12.1
// Install Monzo as a Cake Tool
#tool nuget:?package=Monzo&version=0.12.1
Monzo.NET
Monzo.NET is a .NET client library for the Monzo bank API. Use it to build apps and view your accounts, balances and transactions, create feed items, manage webhooks and attachments, and more!
>>> Get Monzo.NET via NuGet
Install-Package Monzo
Supported target frameworks: .Net Core, .NET 4.5, ASP.NET Core 5.0, Windows 8, Windows Phone 8.1
Supported Features
- 100% async task-based API
- OAuth 2.0 authentication
- Web application flow (Authorization Code Grant)
- Native CLR types
- Access token refreshing
- Built for unit testing
- List accounts, transactions, and balances
- Create feed items
- Manage webhooks and attachments
- Upload attachments
Usage Examples
Authentication, Accounts, Balances, and Transactions
To authenticate using OAuth 2.0 Web application flow (Authorization Code Grant) and retrieve a list of accounts:
public class HomeController : Controller
{
IMonzoAuthorizationClient _authClient = new MonzoAuthorizationClient(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET);
[HttpGet]
public ActionResult Login()
{
// an unguessable random string which is used to protect against cross-site request forgery attacks
string state = ...;
// the URL the user should be redirected back to following a successful Monzo login
string redirectUrl = Url.Action("OAuthCallback", "Home", null, Request.Url.Scheme);
string monzoLoginPageUrl = _authClient.GetAuthorizeUrl(state, redirectUrl);
// 1. Send user to Monzo's login page
return Redirect(monzoLoginPageUrl);
}
[HttpGet]
public async Task<ActionResult> OAuthCallback(string code, string state)
{
// confirm the redirect url was valid
string redirectUrl = Url.Action("OAuthCallback", "Home", null, Request.Url.Scheme);
// 2. Exchange authorization code for access token
AccessToken accessToken = await _authClient.GetAccessTokenAsync(code, redirectUrl);
// 3. Begin fetching accounts, transactions etc
using (var client = new MonzoClient(accessToken.Value))
{
IList<Account> accounts = await client.GetAccountsAsync();
// ... etc
}
}
}
Validating your API token
To check if your access token is valid and authenticated:
// List access token info
await client.WhoAmIAsync();
Feed Items
To create a feed item:
// create feed item
var parameters = new Dictionary<string, string>
{
{"title", "My custom item"},
{"image_url", "www.example.com/image.png"},
{"background_color", "#FCF1EE"},
{"body_color", "#FCF1EE"},
{"title_color", "#333"},
{"body", "Some body text to display"},
};
await client.CreateFeedItemAsync("myaccountid", "basic", parameters, "https://www.example.com/a_page_to_open_on_tap.html");
Webhooks
To register, delete and list webhooks:
// list webhooks
IList<Transaction> webhooks = await client.ListTransactionsAsync("myaccountid");
// register new webhook
Webhook webhook = await client.RegisterWebhookAsync("myaccountid", "http://example.com/webhook");
// delete webhook
await client.DeleteWebhookAsync(webhook.Id);
Attachments
To upload, register and remove transaction attachments:
// upload and register an attachment
using (var stream = File.OpenRead(@"C:\example.png"))
{
Attachment attachment = await client.UploadAttachmentAsync("example.png", "image/png", transaction.Id, stream);
}
// register an attachment that is already hosted somewhere
Attachment attachment = await client.RegisterAttachmentAsync(transaction.Id, "http://example.com/pic.png", "image/png");
// remove attachment
await client.DeregisterAttachmentAsync(attachment.Id);
Refreshing your Access Token
OAuth 2.0 access tokens expire and must be periodically refreshed to maintain API access. Here is an example using an Rx IScheduler:
private _refreshDisposable = new SerialDisposable();
// schedule automatic token refresh
private void EnqueueRefresh()
{
DateTimeOffset refreshTime = DateTimeOffset.UtcNow.AddSeconds(_accessToken.ExpiresIn);
_refreshDisposable.Disposable = Scheduler.Default.Schedule(refreshTime, async () =>
{
await _authClient.RefreshAccessTokenAsync(_accessToken.RefreshToken);
EnqueueRefresh();
});
}
Samples
ASP.NET MVC
Check out the ASP.NET MVC Web Application Sample demonstrating OAuth 2.0 Web application flow (Authorization Code Grant):
https://github.com/rdingwall/MondoAspNetMvcSample
Universal Windows
Also the Universal Windows Sample application using Monzo.NET, Rx and MVVM:
https://github.com/rdingwall/MondoUniversalWindowsSample
Contributions
Contributions and pull requests are more than welcome! 🎁
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard1.1 netstandard1.2 netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1 |
.NET Framework | net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen30 tizen40 tizen60 |
Universal Windows Platform | uap uap10.0 |
Windows Phone | wpa81 |
Windows Store | netcore netcore45 netcore451 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 1.1
- NETStandard.Library (>= 1.6.1)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.