DEV Community

Cover image for Writing a simple API with Deno

Posted on • Updated on

Writing a simple API with Deno

Today we gonna write a REST API using Deno.

Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. You can look more about Deno on his official website

We're not here to fight and compete between Deno and Node, we're here to learn something new.


  1. Install Deno.
  2. Create API
  3. Run the API with permissions.
  4. Test our API

1. Install Deno

With Shell:

$ curl -fsSL | sh -s v1.0.2
Enter fullscreen mode Exit fullscreen mode

With PowerShell:

iwr -useb -outf install.ps1; .\install.ps1 v1.0.2
Enter fullscreen mode Exit fullscreen mode

2. Create API

Open a text editor, and create a file called index.ts. Add this to the index.ts file.

// Import the oak utils for app and router. Oak works similar than Express in Node, we are using the version 4.0.0 of oak
import { Application, Router } from ''

// Let use the host parameters, but we set default values
const PORT = 8000
const HOST = 'localhost'

// Start instances of app and router
const app = new Application()
const router = new Router()

// This API will have only an get method
router.get('/api', (context) => {
  context.response.body = 'Hello from Deno API!'

// We let the app use the routes define above

// Start the app in the host and the port setted
const HOST_PORT = `${HOST}:${PORT}`
console.log(`Listen on ${HOST_PORT}`)
Enter fullscreen mode Exit fullscreen mode

3. Run the API with permissions.

Deno has a feature to explicitly ask for permission to use the computer resources, protocols, and more.

To run the API we need to set the --allow-net flag to use the network protocols in our app. Now, run this script in your path where the index.ts is.

$ deno run --allow-net ./index.ts 
Enter fullscreen mode Exit fullscreen mode

You will see something like this:
output deno api console

4. Test our API

Now we can go to our browser and test the API at http://localhost:8000.

You will see something like this:

output deno api broser

Wrapping up

This is a simple example of how to create a simple API in Deno.

I invite you to explore new things like Deno, use your energy on learning new things, and ovoid as much as you can the comparison between Deno and other tools.

Remember that this code is just an example, take this implementation as a reference to build your own solid, secure, and scalable solutions.

If you're more curious about Deno, I recommend these resources:

Build a Chat app with Deno
The Deno Handbook
Deno — How’s it Different to Node.js and Should I Learn it?

Happy coding!

Latest comments (4)

galkowskit profile image
Tomasz Gałkowski

Note: this article contains bad practices. When importing any library in Deno you should version lock your import statements. Otherwise, your application will break very quickly.

This is 101 of Deno and is one of the first things you'd read in "Getting started" of Deno Manual.

luispa profile image

Thanks for the observation.

Now the post uses the latest version of oak for the same code.

import { Application, Router } from ''
iamareebjamal profile image
Areeb Jamal

Yup, Deno guides and tutorials, and preferably the runtime itself should issue a strict warning about such usages. Or else, what happened in PHP ecosystem will repeat in Deno, beginners created sites with SQL and XSS injection because PHP just allowed it. Similarly, beginners will just create projects which work for a week and then are broken, because they didn't version the imports and load them from a single file.

raagpc profile image
raag pc

Nice post, thank you