Onfleet API: Last mile deliveries in 7 lines of code

TribalScale Inc.
TribalScale
Published in
3 min readJan 21, 2021

by Will Ginsberg, Agile Software Engineer

When you choose a platform for your last mile delivery operation consider Onfleet. They offer a fully featured dashboard for scheduling and managing deliveries as well as an app for drivers. But, as we’ll see, the real power of the Onfleet platform is unlocked when you use their API to automate delivery operations.

Behold, a last mile delivery in 7 lines of NodeJS:

const Onfleet = require(‘@onfleet/node-onfleet’)
const onfleet = new Onfleet(‘d326789cafbc6276c542f1e860802e7e’)
onfleet.tasks.create({
destination: { address: { unparsed: ‘2 Yonge St, Toronto, ON’}},
recipients: [{ name: ‘Will Ginsberg’, phone: ‘+19055559048’
}],
container: { type: ‘TEAM’, team: ‘AEXMHmzzIMMKMDCAPUZPdsdW’ },
barcodes: [{ data: ‘dHJpYmFsc2NhbGU=’, blockCompletion: true
}]
})

Let’s break down each line:

const Onfleet = require(‘@onfleet/node-onfleet’)
const onfleet = new Onfleet(‘d326789cafbc6276c542f1e860802e7e’)

Initializing the NodeJS wrapper for Onfleet’s API. You can choose from NodeJS, Python, or plain HTTP requests.

Note that you’ll need to sign up for a trial account and get your own API key with which to try this example:

onfleet.tasks.create

A task in Onfleet is an instruction for a driver to make a stop at a particular location. A task can include special notes for the driver, and specify requirements like collecting a signature on delivery.

destination: { address: { unparsed: ‘2 Yonge St, Toronto, ON’}}

The location the driver will need to arrive at: the easiest way to get started with destinations is to send Onfleet an unparsed string of text as we’ve done above, but it is advisable to specify a fully structured address object including latitude, longitude.

recipients: [{ name: ‘Will Ginsberg’, phone: ‘+19055550952’ }]

The persons who will be receiving the delivery: Onfleet will send notifications and tracking information by SMS to the recipient and allow the driver to contact them via a masked phone service in the app.

container: { type: ‘TEAM’, team: ‘AEXMHmZZImmKMDACPUPZdsdW’ }

Currently a beta feature of Onfleet, here we make this task available for any driver on our specific team to claim the task. We could also use automatic assignment, where Onfleet would attempt to assign the task to a currently active driver.

barcodes: [{ data: ‘dHJpYmFsc2NhbGU=’, blockCompletion: true }]

Here we specify a list of barcodes the driver must scan in Onfleet driver app. Other barcode scanning scenarios are supported as well, such as allowing the driver to proceed after scanning different barcodes than what is specified here.

The end result is a delivery that we can see in the dashboard:

And our driver team can view, claim, and start the task:

There we have it! Last mile delivery done in 7 lines of code. If you’re interested in exploring the Onfleet API in more detail, you can check out their developer docs here: https://docs.onfleet.com/. And if you’re looking for an end to end solution for delivery in your business, reach out to Tribalscale, we’d love to chat.

Will is an Agile Software Engineer at TribalScale where he works with a variety of web and internet technologies. He is passionate about learning and teaching others how to improve accessibility. You can usually find him riding his bike up and down Toronto’s waterfront or watching bad reality TV.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

--

--

TribalScale Inc.
TribalScale

A digital innovation firm with a mission to right the future.