plaid-node
A node.js client library for the Plaid API.
Table of Contents
Install
$ npm install plaid
Versioning
You can specify the Plaid API version you wish to use when initializing plaid-node
. Releases prior to 2.6.x
do not support versioning.
const plaidClient = new plaid.Client({
clientID: process.env.PLAID_CLIENT_ID,
secret: process.env.PLAID_SECRET,
env: plaid.environments.sandbox,
options: {
version: '2019-05-29',
},
});
For information about what has changed between versions and how to update your integration, head to the API upgrade guide.
Getting started
The module supports all Plaid API endpoints. For complete information about the API, head
to the docs.
All endpoints require a valid client_id
and secret
to
access and are accessible from a valid instance of a Plaid Client
:
const plaid = require('plaid');
const plaidClient = new plaid.Client({
clientID: client_id,
secret: secret,
env: plaid_env,
});
The plaid_env
parameter dictates which Plaid API environment you will access. Values are:
The options
field is optional and allows for clients to override the default options used to make requests. e.g.
const patientClient = new plaid.Client({
clientID: client_id,
secret: secret,
env: plaid_env,
options: {
timeout: 30 * 60 * 1000,
version: '2019-05-29',
}
});
See here for a complete list of options. The default timeout for requests is 10 minutes.
Methods
Once an instance of the client has been created you use the following methods:
const plaid = require('plaid');
const plaidClient = new plaid.Client({
clientID: client_id,
secret: secret,
env: plaid_env,
options: {
version: '2019-05-29',
},
});
plaidClient.createPublicToken(access_token, cb);
plaidClient.exchangePublicToken(public_token, cb);
plaidClient.createProcessorToken(access_token, account_id, processor, cb);
plaidClient.invalidateAccessToken(access_token, cb);
plaidClient.removeItem(access_token, cb);
plaidClient.getItem(access_token, cb);
plaidClient.importItem(products, user_auth, options, cb)
plaidClient.updateItemWebhook(access_token, webhook, cb);
plaidClient.getAccounts(access_token, options, cb);
plaidClient.getBalance(access_token, options, cb);
plaidClient.getAuth(access_token, options, cb);
plaidClient.getIdentity(access_token, cb);
plaidClient.getIncome(access_token, cb);
plaidClient.getCreditDetails(access_token, cb);
plaidClient.getLiabilities(access_token, cb);
plaidClient.getDepositSwitch(deposit_switch_id, options, cb)
plaidClient.createDepositSwitch(target_account_id, target_access_token, options, cb);
plaidClient.createDepositSwitchToken(deposit_switch_id, options, cb)
plaidClient.getHoldings(access_token, cb);
plaidClient.getInvestmentTransactions(access_token, start_date, end_date, options, cb);
plaidClient.getTransactions(access_token, start_date, end_date, options, cb);
plaidClient.getAllTransactions(access_token, start_date, end_date, options, cb);
plaidClient.refreshTransactions(access_token);
plaidClient.createStripeToken(access_token, account_id, cb);
plaidClient.getInstitutions(count, offset, options, cb);
plaidClient.getInstitutionById(institution_id, options, cb);
plaidClient.searchInstitutionsByName(query, products, options, cb);
plaidClient.getCategories(cb);
plaidClient.getWebhookVerificationKey(key_id, cb);
plaidClient.resetLogin(access_token, cb);
plaidClient.sandboxItemFireWebhook(access_token, webhook_code, cb);
plaidClient.sandboxItemSetVerificationStatus(access_token, account_id, verification_status, cb);
plaidClient.sandboxPublicTokenCreate(institution_id, initial_products, options, cb);
All parameters except options
are required. If the options parameter is omitted, the last argument to the function
will be interpreted as the callback.
Callbacks
All requests have callbacks of the following form:
function callback(err, response) {
}
Error Handling
The err
argument passed to either callback style can either be an instance of Error
or a Plaid API error object. An Error
object
is only passed back in the case of a HTTP connection error. The following code distinguishes
between a Plaid error and a standard Error instance:
function callback(err, response) {
if (err != null) {
if (err instanceof plaid.PlaidError) {
console.log(err.error_code + ': ' + err.error_message);
} else {
console.log(err.toString());
}
}
}
Examples
Exchange a public_token
from Plaid Link for a Plaid access_token
and then
retrieve account data:
plaidClient.exchangePublicToken(public_token, function(err, res) {
const access_token = res.access_token;
plaidClient.getAccounts(access_token, function(err, res) {
console.log(res.accounts);
});
});
Retrieve transactions for a transactions user for the last thirty days:
const now = moment();
const today = now.format('YYYY-MM-DD');
const thirtyDaysAgo = now.subtract(30, 'days').format('YYYY-MM-DD');
plaidClient.getTransactions(access_token, thirtyDaysAgo, today, (err, res) => {
console.log(`You have ${res.transactions.length} transactions from the last thirty days.`);
});
Get accounts for a particular Item
:
plaidClient.getAccounts(access_token, {
account_ids: ['123456790']
}, (err, res) => {
console.log(res.accounts);
});
plaidClient.getAccounts(access_token, (err, res) => {
console.log(res.accounts);
});
Promise Support
Every method returns a promise, so you don't have to use the callbacks.
API methods that return either a success or an error can be used with the
usual then/else
paradigm, e.g.
plaidPromise.then(successResponse => {
}).catch(err => {
});
For example:
'use strict';
const bodyParser = require('body-parser');
const express = require('express');
const plaid = require('plaid');
const plaidClient = new plaid.Client({
clientID: process.env.PLAID_CLIENT_ID,
secret: process.env.PLAID_SECRET,
env: plaid.environments.sandbox,
options: {
version: '2018-05-22',
},
});
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/plaid_exchange', (req, res) => {
var public_token = req.body.public_token;
return plaidClient.exchangePublicToken(public_token)
.then(res => res.access_token)
.then(accessToken => plaidClient.getAccounts(accessToken))
.then(res => console.log(res.accounts))
.catch(err => {
if (!(err instanceof plaid.PlaidError)) {
res.sendStatus(500);
return;
}
console.log('/exchange token returned an error', {
error_type: err.error_type,
error_code: res.statusCode,
error_message: err.error_message,
display_message: err.display_message,
request_id: err.request_id,
status_code: err.status_code,
});
switch(err.error_type) {
case 'INVALID_REQUEST':
break;
case 'INVALID_INPUT':
break;
case 'RATE_LIMIT_EXCEEDED':
break;
case 'API_ERROR':
break;
case 'ITEM_ERROR':
break;
default:
}
res.sendStatus(500);
});
});
app.listen(port, () => {
console.log(`Listening on port ${ port }`);
});
Support
Open an issue!
Contributing
Click here!
License
MIT