JavaScript
This is a simple example which shows communicating with the server to retrieve /me user info as well as update SCM data for a set of codes.
Shows the synchronous and asynchronous methods for the API access.
Requires the axios
package, which needs to be installed with npm install
//
// Node.js example as found on https://devportal.scantrust.com
//
//
const axios = require('axios')
//
// set the below two based on your token / environment.
// two options are: STAGING: api.staging.scantrust.io or PRODUCTION: api.scantrust.com
//
const API_SERVER = 'https://api.staging.scantrust.io' // ensure no trailing "/"
const UAT_TOKEN = 'ENTER_YOUR_UAT_TOKEN_HERE' // UAT token created by the user on the portal
// set the UAT token for every request
axios.defaults.headers.common['Authorization'] = `UAT ${UAT_TOKEN}`
axios.defaults.baseURL = API_SERVER
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
//
// EXAMPLE SCENARIO:
// 1. call sync first: update qr codes with a pallet number
// 2, call async to update based on pallet number
// 3. poll the task to see if it's completed.
//
;(async () => {
//
// optional: call the /me endpoint to see who you are
//
let response = await axios.get(`/api/v2/me`)
let me = response.data
console.log(
`UAT token is valid for user: ${me.email} (${me.id} ) as ${me.role} for company ${me.company.name} (${me.company.id})`
)
//
// Example 1: synchronous scm update
// Pass a list of items, codes will be updated one-by-one based on the data_key
//
// replace this list with real codes from your campaign.
//
const uploadDataSync = {
data_key: 'extended_id',
items: [
{ extended_id: 'C0678AB9B80410MRP0410FCF4264BBF', pallet_number: 'PALLET_001' },
{ extended_id: '85C7C6504B0410MRP04106924D94990', pallet_number: 'PALLET_001' },
{ extended_id: '91D819942D0410MRP0410FB1427B87F', pallet_number: 'PALLET_001' },
{ extended_id: '830119BC410410MRP0410FC44A34834', pallet_number: 'PALLET_001' },
],
}
let syncResponse = await axios.post(`/api/v2/scm/upload/`, uploadDataSync)
console.log(
`Sync update returned ${syncResponse.status} (${syncResponse.statusText}):`,
syncResponse.data
)
//
// Example 2: asynchronous scm update
// Pass a constraint
// all codes matching that constraint will be updated
// because this can take a while if the number of codes is big, returns a task with an id
// then poll the task
//
const uploadDataAsync = {
constraints: {
pallet_number: ['PALLET_001'],
},
scm_data: {
shipment_number: `SHIPMENT_001`,
},
// campaign: 11, // optional. Add a campaign-ID to prevent codes in other campaigns from being updated.
reference: 'SCM upload reference ' + Date.now(), // optional. Pass in a reference for easy lookup later
}
let asyncResponse = await axios.post(`/api/v2/scm/upload/async/`, uploadDataAsync)
console.log(
`Async update returned ${asyncResponse.status} (${asyncResponse.statusText}):`,
asyncResponse.data
)
let task_id = asyncResponse.data.task_id
let task = (await axios.get(`/api/v2/scm/tasks/${task_id}/`)).data
//
// Poll every 5 seconds to see if the task has completed
//
while (task.state === 'in-progress') {
console.log('task in progress...')
await delay(5000) // wait 5 seconds
task = (await axios.get(`/api/v2/scm/tasks/${task_id}/`)).data
}
console.log('task complete:')
console.log(task)
})().catch((err) => console.log(err.stack))