How to Create REST API With Deno

In this article, we’ll be looking at creating a REST API using Deno, Oak framework and its Router. We will also be testing our API using Postman in Chrome or RESTClient in Firefox.

In the previous article, we have learned about what is deno, it’s advantages. If you haven’t read yet check here.

Let’s take a look at the API we want to build and what it can do.

Let’s build an API that will:

  • Handle CRUD for an Locations.
  • Use the proper HTTP verbs to make it RESTful (GET, POST, PUT, and DELETE)
  • Return JSON data.

Installation:

I am using Windows to develop this REST API. To install Deno on your machine just type the below command mentioned command in the terminal.

PowerShell (Windows):

See here for other operating system installation guide.

To verify if the deno is installed or not. Just type deno --version on your terminal it will show an install version of Deno.

Getting Started

Here is our file structure. We won’t need many files and we’ll keep this very simple.

  • Controllers: has a logic of the application and handles the client requests.
  • Routes.ts: containing API routes.
  • Server.ts: code to run localhost server.
  • Types.ts: contain the model definition.

At any point in time, if you are stuck somewhere, you can follow this link to get the complete code.

Setting Up Our Server (server.ts)

create server.ts file and import Application from https://deno.land/x/oak/mod.ts and setup the port.

We will add our routes in the two methods .use() and .listen

router.routes() — It allows all the routes defined in the routes.ts file through the router object.

router.allowedMethods() — It allows all the methods used in Oak routing such as GET, POST, PUT, DELETE.

Set up routes

Now let’s create the routes.ts file and import Router from https://deno.land/x/oak/mod.ts. and create routes for our API.

Create model

Now let’s create type.ts file

This file pertains the interface of the location.

Create our locations

We will create an initial array of locations object in locations.ts file.

Now we will create different methods one by one in location.ts file and test it with Postman.

getLocations It will return all locations on list. As stated above. Making GET request on /api/v1/locations/

Testing on postman

Deno API Postman GET ALL Location

getLocation: It will return the single location by Id and error if the product not found. Making GET request on /api/v1/locations/:id

Testing on Postman

Deno API Postman Get Single Location
Deno API Postman No Location found

addlocation: It will add the location to the list. Making POST request on /api/v1/locations.

Deno API Postman POST Craete Location

updateLocation: It will update the location. Making PUT request on /api/v1/locations/:id.

Deno API Postman Update Record

deleteLocation: This will delete the location from the list. Making the DELETE on /api/v1/locations/:id

Deno API Postman Delete Location

After creating these functions, we need to import it to the router file. complete the locations.ts by exporting these functions.

Conclusion

We now have the means to handle CRUD on a specific resource through our own API. Using the above techniques above will be a good foundation to move into building larger and most robust APIs.

This has been a quick look at creating a Deno Rest API using Oak. There are many more things you can do with your own APIs. You can add authentication, create better error messages, and add different sections.

If you have suggestions please let me know in the comments section🙋‍♂️

Happy Coding 😃

Thank You! 😍

Here is the GitHub Repository for all the source code.

Full stack Developer- I help aspiring and established web developers take their coding skills to the next level.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store