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.

5 Likes

Thanks for your examples. I have used it to create an R version for use with ruODK.

4 Likes

Fantastic work here, guys!

You saved my life while I was struggling to find a way to generate the appropriate data to put in that QR Code. Here's my contribution to this post: a Python implementation of such mechanism.

2 Likes

Is there an update to the JSON parameters for the new version of ODK colelct i.e v2021. I'd like to set project name, icon or color.

If anyone needs an updated version, @LN has is a Python implementation that uses our official pyODK library. See https://gist.github.com/lognaturel/d538a40901aad8e5057bf0aeb8081ea6.

1 Like