UNPKG

7.38 kBMarkdownView Raw
1# bunqJSClient
2[![NPM Version](https://img.shields.io/npm/v/@bunq-community/bunq-js-client.svg) ](https://github.com/@bunq-community/bunq-js-client)
3[![NPM Downloads](https://img.shields.io/npm/dt/@bunq-community/bunq-js-client.svg) ](https://www.npmjs.com/package/@bunq-community/bunq-js-client)
4[![build status for master branch](https://api.travis-ci.org/bunqCommunity/bunqJSClient.svg?branch=master) ](https://travis-ci.org/bunqCommunity/bunqJSClient)
5[![MIT License](https://img.shields.io/npm/l/@bunq-community/bunq-js-client.svg)](https://github.com/bunqCommunity/bunqJSClient/blob/master/LICENSE)
6[![codecov](https://codecov.io/gh/bunqCommunity/bunqJSClient/branch/master/graph/badge.svg) ](https://codecov.io/gh/bunqCommunity/bunqJSClient)
7
8A unofficial javascript SDK for the bunq API. It is aimed at allowing single page applications to do all interactions with bunq without proxying through other services.
9
10The API session details are encrypted and stored using [forge](https://github.com/digitalbazaar/forge).
11
12This project was originally built for the browser but has since then been tested and used with NodeJS servers. If you do want to use NodeJS you can still easily create a custom storage handler (with the default being Localstorage) like described in the [installation](#installation) section.
13
14## Installation
15Install the library
16```bash
17yarn add @bunq-community/bunq-js-client
18```
19
20Next create a new instance with an optional storage interface as the first parameter. This defaults to [store.js](https://github.com/marcuswestin/store.js/) but any class
21with the following methods: `get(key)`, `set(key, data)`, `remove(key)`.
22
23## Usage
24Create a new client using LocalStorage.
25```js
26const bunqJSClient = new BunqJSClient();
27```
28
29The default installation attempts to use LocalStorage which is only compatible with the browser. You can check the `src/Stores/*` folder for other compatible storage handlers. This example uses the JSON store which writes the data to a local JSON file.
30```js
31import JSONFileStore from "@bunq-community/bunq-js-client/dist/Stores/JSONFileStore";
32
33// run the file store with a location to store the data
34const storageInstance = JSONFileStore("./bunq-js-client-data.json");
35
36// create a new bunqJSClient with the new storage instance
37const bunqJSClientCustom = new bunqJSClient(storageInstance);
38
39// disables the automatic requests to keep the current session alive
40// instead it'll create a new session when it is required
41bunqJSClient.setKeepAlive(false);
42```
43
44Next run the setup functions to get started
45```js
46/**
47 * A 16-byte encryption key, check the examples (create_encryption_key.js)
48 * on how to create one
49 * @see https://github.com/digitalbazaar/forge#pkcs5
50 */
51const ENCRYPTION_KEY = "3c7a4d431a846ed33a3bb1b1fa9b5c26";
52const API_KEY = "abcd-1234-abcd-1234"; // Your bunq API key
53/**
54 * The device name which will show in the installation notification that bunq sends
55 * this also lets users manage their keys more easily
56 */
57const DEVICE_NAME = "My Device";
58const ENVIRONMENT = "SANDBOX"; // OR you can use PRODUCTION
59
60/**
61 * Permitted IPs, allowed values are:
62 * - Empty if you're not sure (bunq will use the current IP)
63 * - An array of allowed IP addresses
64 * - The "*" character to enable wildcard mode
65 */
66const PERMITTED_IPS = [];
67
68const setup = async () => {
69 // run the bunq application with our API key
70 await bunqJSClient.run(API_KEY, PERMITTED_IPS, ENVIRONMENT, ENCRYPTION_KEY);
71
72 // install a new keypair
73 await bunqJSClient.install();
74
75 // register this device
76 await bunqJSClient.registerDevice(DEVICE_NAME);
77
78 // register a new session
79 await bunqJSClient.registerSession();
80}
81```
82
83Now you can use the API in the bunq client to do requests and get the current users.
84```js
85// force that the user info is retrieved from the API instead of the data currently in the object
86const forceUpdate = true;
87
88// all users connected to the api key
89const users = await bunqJSClient.getUsers(forceUpdate);
90
91// get only the userCompany account if one is set
92const userCompany = await bunqJSClient.getUser("UserCompany", forceUpdate);
93
94// get all payments for a user and monetary account
95const payments = await bunqJSClient.api.payment.list(userId, accountId);
96```
97
98## OAuth authentication
99You can use the helper function to format a correct url to start the login flow:
100```js
101const url = bunqJSClient.formatOAuthAuthorizationRequestUrl(
102 clientId,
103 redirectUri,
104 optionalState = false
105);
106```
107
108Next when the user grants access use the returned code parameter with:
109```js
110const authorizationCode = await bunqJSClient.exchangeOAuthToken(
111 clientId,
112 clientSecret,
113 redirectUri,
114 code,
115 grantType: string = "authorization_code",
116 state: string | false = false
117)
118```
119
120This will return the if successful `access_token` which is a valid API key. Using this key will give you access to the limited `UserApiKey` user object. For more details on the limitations of a OAuth connection check out the official together topic [here](https://together.bunq.com/d/3016-oauth).
121
122## Examples
123There are a few examples which can be found in the `examples/` folder. `create_sandbox_apikey` will create and output a new sandbox key which you can use with the other examples.
124
125The examples use [dotenv](https://github.com/motdotla/dotenv) so make sure to copy the `.env.example` file to `.env` and enter the correct values.
126
127## Supported APIs
128For more details look into the endpoints found at `src/Api/*`. Adding endpoints is relatively easy but they tend to get added when required or requested. The most common endpoints are now all implemented but feel free to request (Or preferably create a pull request) for any endpoints that are missing.
129
130## Contact
131[![Telegram chat badge](https://img.shields.io/badge/Telegram-Discuss-blue.svg) ](https://t.me/bunqcommunity)
132
133We have a public [Telegram chat group ](https://t.me/bunqcommunity). Feel free to create a new issue for any suggestions, bugs or general ideas you have on Github or contact us through one of the above.
134
135## Contributors ![Contributer count](https://img.shields.io/github/contributors/bunqcommunity/bunqjsclient.svg)
136
137[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/0)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/0)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/1)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/1)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/2)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/2)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/3)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/3)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/4)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/4)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/5)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/5)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/6)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/6)[![](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/images/7)](https://sourcerer.io/fame/crecket/bunqCommunity/bunqJSClient/links/7)