wa-business-api
v1.0.3
Published
Utility helper for exchanging a WhatsApp Business authorization code for an access token using the Facebook Graph API.
Downloads
29
Readme
wa-business-api
Utility helper for exchanging a WhatsApp Business authorization code for an access token using the Facebook Graph API.
Installation
npm install wa-business-apiRequirements
- Node.js 18 or later (for native
fetchsupport). If you are on an earlier version, polyfill the globalfetch. - A Meta app with WhatsApp Business API access.
Usage
const { WhatsAppOauthCodeExchange } = require('wa-business-api');
async function getAccessToken(code) {
try {
const token = await WhatsAppOauthCodeExchange(code, {
client_id: process.env.WA_CLIENT_ID,
client_secret: process.env.WA_CLIENT_SECRET,
redirect_uri: process.env.WA_REDIRECT_URI, // optional
version: 'v21.0', // optional, defaults to v21.0
});
console.log(token);
} catch (error) {
console.error('Token exchange failed', error);
}
}Function signature
WhatsAppOauthCodeExchange(
code: string,
options: {
client_id: string;
client_secret: string;
version?: string; // defaults to v21.0
redirect_uri?: string;
}
): Promise<{
access_token: string;
token_type?: string;
expires_in?: number;
}>;Error handling
- Throws if any required parameter is missing.
- Throws a custom
Errorwhen the Graph API returns a non-2xx response. The error includeserror.statusanderror.details(if provided by Graph API).
Development
npm install
npm testTests are not yet implemented (npm test exits with an error until you add them).
Publishing to npm
Ensure you are logged in to npm:
npm loginBump the version in
package.jsonfollowing semver rules:npm version <major|minor|patch>Check that the package builds correctly:
npm packInspect the generated tarball to confirm that only the intended files are included.
Publish:
npm publishTag the release in git (optional but recommended):
git tag v$(node -p "require('./package.json').version") git push origin --tags
License
ISC
