ATTENTION: These docs have been deprecated. Please visit our new developer portal for updated information.

Sample Code: User JWT

Here is some example code in Javascript for how to work with the user JWTs. It uses bitcore-lib, adapted for readability from the code running this site:

const Mnemonic = require('bitcore-mnemonic');
const bitcore = require('bitcore-lib');
const Buffer = bitcore.deps.Buffer;

const clientJWT = process.env.BLOCKSET_CLIENT_JWT;

// initialize your wallet keys from mnemonic:
const mnemonic = new Mnemonic("win buffalo latin awesome roof...");

// derive your API public/private key:
const authKey = mnemonic.toHDPrivateKey.deriveChild(`m/1'/0`);

// store the public & private keys for later:
const publicKey = authKey.publicKey;
const privateKey = authKey.privateKey;

// function for signing (ECDSA/SHA256) with above keys:
function sign(string) {
  const hash = bitcore.crypto.Hash.sha256(Buffer.from(string,'utf-8'));
  return bitcore.crypto.ECDSA.sign(hash, privateKey);

// Body of request that will go to create a new user:
const userTokenRequest = {
  "signature": sign(clientJWT).toDER().toString('base64'),
  "pub_key": publicKey.toDER().toString('base64'),
  "device_id": state.deviceId,

// simple isomorphic network library:
const unet = require('unet');

// Create user:
const { error , result } = await unet({
  method: 'POST'
  url: '/api/users/token',
  body: userTokenRequest,
  headers: {
    'authorization': `Bearer ${clientJWT}`,

// Get back user uuid and client ID:
const userToken = result.token;
const clientId = result.client_token;

if(error) {
  // bail!

// function to convert normal base64 to base64url:
function base64ToBase64URL(str) {
  return str.replace(/\+/g,'-').replace(/\//g,'_').replace(/\=/g,'');

function jwtEncode(object) {
  return base64ToBase64URL(Buffer.from(JSON.stringify(object)).toString('base64'));

const now = () => (new Date()).getTime() / 1000;

const jwtHeader = jwtEncode({ alg: 'ES256', typ: 'JWT' });

const jwtObject = {
  sub: result.token,
  iat: now(),
  exp: now()+60*60,
  'brd:ct': 'usr',
  'brd:cli': clientId,

const jwtBody = jwtEncode(jwtObject);

const jwtMain = `${jwtHeader}.${jwtBody}`;

// you can also do the JWT construction process with a
// JWT library, here we do it more directly to make it clear
// what is happening:

const jwtSignature = sign(jwtMain);
const Formatter = require('ecdsa-sig-formatter');
const jwtSignatureString = Formatter.derToJose(jwtSignature.toDER(),'ES256');

const userJWT = `${jwtMain}.${jwtSignatureString}`;

// Done!
EU Flag

This site uses cookies for the purposes outlined in our cookie policy. Your consent is assumed by dismissing this banner.