API Client for Accessing a Badgr Server
npm install @geobadges/badgr-api-client
Your contributions are welcomed! This repo is open to issues and pull requests.
const API = require("@geobadges/badgr-api-client");
const client = new API({ endpoint, password, username });
const {
accessToken,
expirationDate,
refreshToken
} = await client.getAccessToken();
// accessToken is asdfasdyfbasbdif6basdf
const entityId = '12asd8f7glasi8df7'; // badge id
const fields = ['name', 'entityId', 'criteriaNarrative', 'tags'];
const badge = await api.getBadge({
entityId,
fields
})
const data = {
agreedToTermsOfService: true,
email: "[email protected]",
firstName: "First Name",
lastName: "Last Name",
optedInToMarketing: false,
password: "acomplexpassword",
};
const sucessful = await client.register(data);
// sucessful is true
const data = {
email: "[email protected]"
};
const successful = await client.requestPasswordReset(data);
// successful is true if HTTP request, requesting a reset, returned successfully
If you don't supply an entityId, it automatically returns the user that you used to initialize the api client.
/*
The following fields are available:
entityType, entityId, firstName, lastName, emails, url,
telephone, agreedTermsVersion, hasAgreedToLatestTermsVersion,
marketingOptIn, badgrDomain, hasPasswordSet, and recipient
*/
const fields = ['emails', 'entityId', 'firstName', 'lastName'];
const user = await api.getUser({
entityId,
fields
})
const entityId = '12asd8f7glasi8df7'; // badge id
const fields = ['entityId', 'recipient']; // fields to include in return
const assertions = await client.getBadgeAssertions({ entityId, fields });
const fields = ['entityId', 'name', 'description']; // fields to include in return
const badges = await client.getBadgeClasses({ fields });
This returns issuers that the current authenticated user has access to unless you pass in an accessToken
const fields = ['entityId', 'name'];
const issuers = await client.getIssuers({ fields });
// an array of issuer objects
const entityId = '12378t12uy3gkj1h2b31';
const fields = ['entityId', 'name'];
const issuer = await client.getIssuer({ entityId, fields });
const fields = ['entityId', 'image'];
const badges = await client.getBackpack({ fields });
// badges is an array of badges
const status = await client.grant({
badgeClassEntityId: "asl8dyb712tyev6tvdsafasdf",
createNotification: false,
email: "[email protected]",
evidence = [],
issuerEntityId: "adsfiubashfv7asgdfasdf",
narrative = "This person earned the badge by creating a JavaScript Notebook that..."
})
Passing in admin: true
will basically add the rw:serverAdmin
scope to all requests.
const client = new API({ endpoint, password, username, admin: true });
Email the package author at [email protected] or post an issue at https://github.com/GeoBadges/badgr-api-client