VOOZH about

URL: https://thenewstack.io/how-to-build-a-serverless-api-with-bun-and-hono/

⇱ How To Build a Serverless API With Bun and Hono - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-10-29 12:01:06
How To Build a Serverless API With Bun and Hono
tutorial,
Frontend Development / JavaScript / Serverless

How To Build a Serverless API With Bun and Hono

Learn to build a fast, modern serverless API with Bun and Hono. This tutorial guides you through setup, coding, and running your application.
Oct 29th, 2025 12:01pm by Jessica Wachtel
👁 Featued image for: How To Build a Serverless API With Bun and Hono
Image via Unsplash. 

There was a time when Node.js was the go-to runtime for JavaScript. For better and sometimes for worse, if you wanted to build a JavaScript application, you were using Node.js for the backend. Because of habit and the huge number of tools, libraries and frameworks that support Node.js, it remains a widely used choice. But in recent years, new runtimes have appeared to address some of the challenges developers face with Node.js.

One of these new backend runtimes is Bun, introduced in 2021. Bun’s elevator pitch is that it is “like Node.js but designed for speed, modern workflows and with fewer tools to install,” which addresses many of the frustrations developers have had with Node.js. This is important because the web in 2025 looks a lot different than it did when Node.js was first created in 2009.

Hono was created in December 2021 by Japanese developer Yusuke Wada. According to its GitHub repository, Hono is “a small, simple and ultrafast web framework built on Web Standards.” It was originally built for Cloudflare Workers, but now works “on any JavaScript runtime,” including Node.js, Deno, Bun and Vercel (although Node support requires an adapter and Node ≥ 18).

Why Build an API with Bun and Hono?

Before we dive into building a serverless API, let’s look at some similar tools now that the marketplace is more competitive.

The Classic: Node.js and Express

This is a heavier — more boilerplate, slower startup times and larger memory footprint — architecture than Bun and Hono.

The Competitor: Deno and Oak

Of all the options out there, Deno and Oak are the closest to Bun and Hono. Deno is a modern runtime — secure defaults, built-in tooling, performance optimizations — with first-class TypeScript support, designed with security and developer-friendly defaults in mind.

So why would you want to build an app with Bun and Hono?

Even in a more competitive marketplace, there are clear benefits to this combination. First, there’s speed. Bun was built specifically for performance, making API responses extremely fast. Then there’s simplicity. The setup is minimal, letting you focus on logic instead of configuration. This architecture is also TypeScript-ready, making type-safe development easier. On top of that, it’s serverless-friendly, with templates for Cloudflare, AWS Lambda and other platforms.

Let’s take it for a test drive.

We’re going to build a simple serverless API that:

  • Receives two numbers from the browser;
  • Calculates the sum;
  • Stores results in memory and in the application.

Prerequisites for This Tutorial

Before starting, you should have the following installed on your machine:

Setting Up Your Bun and Hono Project

We have some installs and basic setup to handle before we start our build. In a new terminal window in a new project in your IDE, let’s install Bun and initialize a new Hono project.

First, we need to install Bun.

After a successful install, we’re ready to initialize the Hono project.

This is where you can see all the templates Hono offers. We’re going to select the “bun” template followed by a “Y” for project dependencies, and the Bun package manager.

For this project, we’re only going to need two files:

  • src/index.ts — This is part of the package we just installed. This page will be our API backend.
  • test.html — You’ll have to add this page in the hono-api file. This is our browser interface.

Building index.ts

All the following code blocks can be added to the same file in this same order. I broke it down to do a deep dive into the functionality of our serverless API.

Imports and Initialization

CORS is the middleware we’ll use. This allows the API to be accessed from a browser, preventing cross-origin issues.

Next, let’s enable CORS. The code below will apply CORS to all routes.

The next thing we’re going to do is create the in-memory storage. When we submit numbers from the browser, this code will allow a results array to also appear in the terminal. Even though we’re saving them in the terminal, they’re still temporary and will clear when you restart the server.

Building the Routes

The first route we’re going to add is our root route. This is a basic GET for the / endpoint.

The next route will be another GET route, this time with a URL parameter. You can use this to test functionality by adding a word to the end of the URL in your browser. The word will appear in the browser’s page.

We’re now ready to add our POST route /api/sum.

This code will receive two numbers from the browser, validate their type, calculate the sum, then store it in the results array. It will log the memory contents in the terminal. This code returns the sum in JSON.

The last route we’re going to build is a GET route, /api/sum.

This will send the code to the browser as JSON.

The last step is to export the app.

Here is the code in one copy/paste option:

Building test.html

test.html is a simple web interface that connects the frontend to the backend. Since it’s basic HTML, we are not going to go over it in depth. This is where you can enter two numbers and add them together, and the sum will be displayed on the page. It also logs the stored results in your terminal.

How to Run Your Serverless Application

Once your build is complete, you can run the app with the command bun run src/index.ts.

You can open the test.html file manually in your browser by double-clicking it in your project folder (on a Mac, I found it using Finder).

The browser will look like this:

👁 Image

And each time you hit “Calculate Sum,” your terminal will update with something that looks like this:

👁 Image

And that’s it! You now have a working serverless sum API with a simple web interface.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Deno.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.