Node.js

Start integrating any APIs with the Bearer clients for Node.js and Express

Installing and initializing

Node.js
Express.js
npm install @bearer/node
npm install @bearer/node
# For WebHooks
npm install @bearer/express
Node.js
Express.js
// Require the Bearer client for NodeJS
const Bearer = require('@bearer/node').default
// Initialize the client with your Bearer API key
const bearer = Bearer(process.env.BEARER_API_KEY)
// Require the Bearer client for NodeJS
const Bearer = require('@bearer/node').default
// Initialize the client with your Bearer API key
const bearer = Bearer(process.env.BEARER_API_KEY)
// Initialize Webhook endpoint
import bearerWebhooks from '@bearer/express'
const EncryptionKey = process.env.BEARER_ENCRYPTION_KEY

Replace the BEARER_API_KEY and BEARER_ENCRYPTION_KEY respectively with your API Key and Encryption Key that can be found on your Developer Keys page.

Calling APIs

Node.js
Express.js
// GET Call
bearer.integration('github')
.get('/repositories', { query: { since: 364 } })
.then(console.log)
.catch(console.error)
// GET Call with a custom SetupId
bearer.integration('github')
.setup(SETUP_ID)
.get('/repositories', { query: { since: 364 } })
.then(console.log)
.catch(console.error)
// POST Call with OAuth
bearer.integration('github')
.auth(authId)
.post('/user/repos', { body: { name: 'Just setting up my Bearer.sh' } })
.then(({data}) => { console.log('Success:', data) })
.catch(err => { console.log('Error: ', err) })
// Adding custom headers
bearer.integration('github')
.setup(SETUP_ID)
.get('/repositories', { headers: { accept: "application/json" }, query: { since: 364 } })
.then(console.log)
.catch(console.error)
// GET call
bearer.integration('github')
.get('/repositories', { query: { since: 364 } })
.then(({ data }) => { console.log(data) });
// GET call with a custom SetupId
bearer.integration('github')
.setup(SETUP_ID)
.get('/repositories', { query: { since: 364 } })
.then(({ data }) => { console.log(data) });
// POST call with OAuth
bearer.integration('github')
.auth(authId)
.post('/user/repos', { body: { name: 'Just setting up my Bearer.sh' } })
.then(({ data }) => { console.log(data) });
// GET call with a custom header
bearer.integration('github')
.setup(SETUP_ID)
.get('/repositories', { headers: { accept: "application/json" }, query: { since: 364 } })
.then(({ data }) => { console.log(data) });

Receiving Webhooks

Express.js
const webhookHandlers = {
['github']: req =>
new Promise(() => {
// you logic goes here
if (something) resolve()
else {
reject()
}
})
}
app.use('/bearer/webhooks', bearerWebhooks(webhookHandlers, { token: EncryptionKey }))

Check out the source of the Node.js & Express clients.