Steps#
1. Load your credentials from the server environment#
Read CLIENT_ID and CLIENT_SECRET from environment variables or your secrets manager. They must never appear in front-end code, a mobile app, a repository or a log line.
2. Create one PluggyClient per process#
The SDK creates an API key with POST /auth and reuses it until it expires, so you don't authenticate on every request. The API key never leaves your server.
3. Expose an endpoint for signed-in users only#
Protect the endpoint with your own authentication. Anyone who can call it can open Pluggy Connect on your behalf.
4. Create a Connect Token for that user#
Pass your internal user id as clientUserId. Use an opaque identifier, not an e-mail address or a CPF: it is stored on the Item and sent in webhook payloads. To update an existing Item, pass its itemId too, after checking that the Item belongs to the signed-in user.
5. Return only the Connect Token#
The front end initializes Pluggy Connect with it. The token is valid for 30 minutes, so create a new one for each connection. See Authentication.
Code#
import express from 'express'
import { PluggyClient } from 'pluggy-sdk'
// 1. Server-side only: environment variables or your secrets manager.
const { PLUGGY_CLIENT_ID, PLUGGY_CLIENT_SECRET } = process.env
// 2. One client per process: it reuses the API key until it expires.
const pluggy = new PluggyClient({
clientId: PLUGGY_CLIENT_ID,
clientSecret: PLUGGY_CLIENT_SECRET,
})
const app = express()
app.use(express.json())
// Replace with your own session or token check.
function requireUser(req, res, next) {
if (!req.user) {
return res.sendStatus(401)
}
next()
}
// Replace with your own lookup: the itemIds you stored for this user.
async function userOwnsItem(userId, itemId) {
return false
}
// 3. Only signed-in users can get a Connect Token.
app.post('/api/pluggy/connect-token', requireUser, async (req, res) => {
const { itemId } = req.body ?? {}
// A Connect Token created with an itemId can update that Item:
// only hand one out for Items that belong to this user.
if (itemId && !(await userOwnsItem(req.user.id, itemId))) {
return res.sendStatus(404)
}
try {
// 4. clientUserId is your opaque internal id, never personal data.
const { accessToken } = await pluggy.createConnectToken(itemId, {
clientUserId: req.user.id,
})
// 5. The front end gets the Connect Token and nothing else.
res.json({ accessToken })
} catch (error) {
console.error('Could not create a Connect Token', error.message)
res.sendStatus(502)
}
})
app.listen(3000)