Generating QR Codes using tokens from Central in JavaScript

Just want to share custom QR Code generation using JavaScript

const serverUrl = YOUR_SERVER
const draftToken = DRAFT_TOKEN_FROM_CENTRAL
const xmlFormId = YOUR_FORM_ID
// the following url construction is for testing draft forms
// use this for generating QR Code for appUser: …/v1/key/[token]/projects/[pid]
const url = serverUrl + '/v1/test/' + draftToken + '/projects/' + pid + '/forms/'+ xmlFormId + '/draft'

// see more keys at https://docs.getodk.org/collect-import-export/#list-of-keys-for-all-settings
const mysettings = {
  general: {
    server_url: `${url}`,
  },
  admin: {},
}

// see http://nodeca.github.io/pako/
var pako = require('pako')
var Uint8Array = new TextEncoder('utf-8').encode(JSON.stringify(mysettings))
var compressedSettings = pako.deflate(Uint8Array, { to: 'string' })
var encodedS64 = btoa(compressedSettings)
console.log(encodedS64 )
// now you can use any QR Code generate library to make QR Code with your own settings.

Hope this helps.

3 Likes