Hono on Azure Functions
Tags: javascript, serverless, typescript, azurefunctions
Published: 2024-05-08 14:39:36

[hono-azurefunc-adapter](https://github.com/Marplex/hono-azurefunc-adapter) is one of the simplest yet incredibly useful js library I have ever written.

Hono is a web application framework built on web standards. It's incredibly fast and lightweight. Because it's built using web standard APIs, the same code will run on multiple runtimes (Cloudflare, Fastly, Deno, Bun, AWS, or Node.js).

For platforms that don't directly support web standards, Hono comes with adapters. For example, running in Node.js requires [an adapter](https://github.com/honojs/node-server) that converts requests and responses into node types and objects.

## Building the Hono adapter

There are a lot of community-made adapters for running Hono on many more environments. Unfortunately, no one has ever made one for Azure Functions. I decided to built it, free and open-source.

The entire library has just only 54 lines of code.
Its so simple and maintainable. Yet it allows to easily port, with minimal/no code rewrites, APIs built with Hono on the powerful Azure Functions platform.

## Simplicity wins

It is incredible to think of how many new possibilities this library unlocks with just 54 lines of code. 

It's true, simple things are always the most difficult.
Although [hono-azurefunc-adapter](https://github.com/Marplex/hono-azurefunc-adapter) now appears clean and concise, it took a while to get to this point. I spent a lot of time polishing, refactoring, and rethinking how to accomplish the same things with fewer lines of code. I had to really dig deep into how the (partly document) Azure Functions API works.

![Hono github star history](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lgrapmkzfnxoxdz4s63.png)

Hono has rapidly become one of the most widely used frameworks for building Javascript web APIs. Hats off to [yusukebe](https://github.com/yusukebe) and all the other contributors! Now it's finally possible to run it on Azure Functions, effortlessly with just `azureHonoHandler(honoApp.fetch)`!

## How to use

It's very simple. Install [hono-azurefunc-adapter](https://github.com/Marplex/hono-azurefunc-adapter) with:

```bash
npm i @marplex/hono-azurefunc-adapter
```

Now create the http trigger for Azure Functions. `honoApp` is your exported Hono application object.

```typescript
import honoApp from "./app";

import { azureHonoHandler } from "@marplex/hono-azurefunc-adapter";
import { app } from "@azure/functions";

app.http("httpTrigger", {
  methods: [
    "GET",
    "POST",
    "DELETE",
    "HEAD",
    "PATCH",
    "PUT",
  ],
  authLevel: "anonymous",
  route: "{*proxy}",
  handler: azureHonoHandler(honoApp.fetch),
});
```

That's it, you're done.

## Limitations

There are some limitations and other things you should keep in mind when running Hono inside Azure Functions.

**Route Prefix**

The default Azure Functions route prefix is `/api`. Be sure to start all your Hono routes with `/api` or change the default Azure Functions route prefix in `host.json`

```json
{
    "extensions": {
        "http": {
            "routePrefix": ""
        }
    }
}
```

**Crypto**

In Node <=18 environments, if you are using `hono/bearer-auth` or any other library that uses crypto, be sure to define `global.crypto = require("crypto");` before registering the http trigger.

**Request signal**

Azure Functions does not expose any signal or event for listening to http request interruptions. `c.req.raw.signal` is useless and its never aborted.

## Conclusion

I think Azure is one of the most trusted enterprise-ready cloud providers. By building [hono-azurefunc-adapter](https://github.com/Marplex/hono-azurefunc-adapter), I hope this will finally allow many to port the same popular Hono APIs to Azure Functions, especially for private enterprise needs.

---

hono-azurefunc-adapter is available on NPM and GitHub Packages. This project is fully open source and MIT licensed, so do what you want! Contributions are welcome 🥳

- GitHub: [https://github.com/Marplex/hono-azurefunc-adapter](https://github.com/Marplex/hono-azurefunc-adapter)
- NPM: [https://www.npmjs.com/package/@marplex/hono-azurefunc-adapter](https://www.npmjs.com/package/@marplex/hono-azurefunc-adapter)
- GitHub Package: [https://github.com/Marplex/hono-azurefunc-adapter/pkgs/npm/hono-azurefunc-adapter](https://github.com/Marplex/hono-azurefunc-adapter/pkgs/npm/hono-azurefunc-adapter)