Sending API Requests

Let's create a method on your Socket Service that interacts with Scatter by sending API requests.

function sendApiRequest(request){
    return new Promise((resolve, reject) => {
        if(request.type === 'identityFromPermissions' && !paired) return resolve(false);

        pair().then(() => {
            if(!paired) return reject({code:'not_paired', message:'The user did not allow this app to connect to their Scatter'});

            // Request ID used for resolving promises
   = random();

            // Set Application Key
            request.appkey = appkey;

            // Nonce used to authenticate this request
            request.nonce = getNextUnhashedNonce();
            // Next nonce used to authenticate the next request
            const unhashedNextNonce = random();
            request.nextNonce = sha256(unhashedNextNonce);

            openRequests.push({, resolve, reject}));

            socket.send('42/scatter,' + JSON.stringify(['api', request]));

Breaking it down
  • We're going to return a promise from this method to help us resolve the request. You'll notice that the entire method is wrapped in a JavaScript Promise.
  • identityFromPermissions is a special API route that is usually called upon every application load right after making a socket connection to Scatter. Because we pair with a passthrough which doesn't prompt the user to link Scatter yet, we check here if request.type == 'identityFromPermissions' is false, and paired is false. If this if statement evalutated to true then we want to resolve the promise to false and exit the method.
  • The next thing we do is to either check if we are paired with Scatter or request a pair linking to be created. If we have the entire Pairing with Scatter section in a promise resolving method called pair() then we can call that here. We should also return and reject if this pairing ultimately resolve to false or no API requests will be accepted by Scatter.
  • = random(); - This just provides a unique ID for this request, which is later used to resolve this promise.
  • request.appkey = appkey; - We also always include the hashed appkey along with every request.
  • request.nonce = getNextUnhashedNonce(); - Every time we send a request we calculate the next nonce and save this unhashed in the persistent local storage of your application. If this is the first request you can make it 0
  • const unhashedNextNonce = random(); - We always calculate the next nonce.
  • request.nextNonce = sha256(unhashedNextNonce); - When you send the next nonce along with the request make sure it is hashed.
  • setNextUnhashedNonce(unhashedNextNonce); - After appending the next nonce to the request don't forget to save this unhashed nonce to your persistent local storage, you will need to send it unhashed along with the next request.
  • openRequests.push(...) - We're pushing this Promise into our openRequests array to be able to resolve it later. Be sure to include the
  • socket.send - We already know this quite well, we're just sending the request to the EVENT type api.