How to build a delivery API with Xano

Tam-Tam

It's been a couple of months since I started learning how to use Xano and I'm very intrigued. Who would have thought you could build APIs without having to write code. In this article, we are going to dabble into the world of APIs - at least from a no-code perspective. It helps if you are a bit familiar with how APIs work and use them sometimes in your no-code applications.

So, what are we building?

What we're building

I wanted to base this build on a simple use case so we are going to be building an API for a fictional delivery company called Shipt. (I actually like that name.)

Because I wanted to keep this simple, I have divided this build into two, the second part would come much later. For this first part, we are going to focus on functionalities to:

  • create a shipment
  • assign a dispatch rider to that shipment and
  • update the status of the shipment.

The next part will include things like authentication, payment for delivery, wallet etc.

Toolstack

For this build, we will only be using Xano.

How it works

On this version of Shipt, there's no need to have an account. You simply create a new shipment by filling out all the necessary details needed. The shipment is then assigned randomly to a dispatch rider and the status of the shipment is updated accordingly from time to time.

It's important to remember that we are working on the backend and so at this point we have no part in how the output or responses of the API are shown on the frontend.

Checklist

First we're going to set up two databases for:

  • Shipments
  • Dispatch Riders

Then we'll work on creating endpoints to:

  • Create a shipment
  • Update a shipment
  • Get a shipment
  • Create dispatch rider
  • Assign shipment
  • Send notification

Let's build

The rider database

For the rider's database, we'll create a basic table with fields for the driver's id, their name, email address, phone number and status; the status lets us know whether they are available or not.

The shipment table

The shipment table is going to have a little more fields; all the basic information you would collect to deliver an item both for the sender and the receiver. We'll also need a relational field linking to the rider's table, this helps us know what rider was assigned to that shipment. We'll need a field for the status of the shipment whether pending, picked up, delivered etc. And finally we'll need an object list field type for the items to be delivered.

Now that we're done with the databases, we can work on the endpoints.You can choose to create most of these endpoints from scratch or use CRUD endpoints automatically generated for each database.

Create a shipment

Start out with the endpoint to create a shipment.

Make sure to test as you are building.

Update a shipment status

Next, create an endpoint to update the status of a shipment. Feel free to use this to edit other details of the shipment but to simplify things, I've made this for just updating the shipment's status.

Again, make sure that you test.

Get a shipment

This endpoint is to fetch details of a particular shipment.

Create a rider

Next, build the endpoint to create a rider. After testing this, make sure you add as many riders as possible. This will help when you're testing the endpoints to assign a shipment and send notifications.

Assign a rider

This is the most complext endpoint needed for this build. Here's how it's going to work:

And here's how to build it function by function:

Query all record of riders

First, query all records of available riders and select one randomly.

If-Then function

Then add a conditional function to return a message if there are no available riders and if there are, edit the shipment with the details of that rider.

Update rider's status

Lastly, add a function to update the status of the rider to unavailable.

Don't forget to test.

Send notification

Finally, the last endpoint in our build. This will be used to notify riders when they have a new shipment to pick up. I made use of Termii to deliver the SMS but you can make use of just about any similar tool.

Here's a picture of the notification I got:

Last things last

And that's how to build a delivery API with Xano. You can build almost any API, whether simple or complex. It's just about the logic.

I realized that we didn't set a way to update a rider's status back to available after they have completed a delivery. Here's how to do it:

  • Add an if-then function to the update shipment endpoint.
  • If the shipment status in the input is equals to delivered then get the details of the driver assigned and edit their status to available.
  • If shipment status is not delivered then do nothing.

I updated that endpoint on my end so you can try it out.

Xano automatically generates API documentation for you on Swagger so here's the one for Shipt. You can try it out and test as much as you want to.

Thanks for reading. If you have any questions, suggestions or need help building an API, feel free to send me an email.