Skip to content

Azure Functions

Azure Functions is a serverless platform from Microsoft Azure. You can run your code in response to events, and it automatically manages the underlying compute resources for you.

Hono was not designed for Azure Functions at first. But with Azure Functions Adapter it can run on it as well.

It works with Azure Functions V4 running on Node.js 18 or above.

1. Install CLI

To create an Azure Function, you must first install Azure Functions Core Tools.

On macOS

sh
brew tap azure/functions
brew install azure-functions-core-tools@4

Follow this link for other OS:

2. Setup

Create a TypeScript Node.js V4 project in the current folder.

sh
func init --typescript

Change the default route prefix of the host. Add this property to the root json object of host.json:

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

INFO

The default Azure Functions route prefix is /api. If you don't change it as shown above, be sure to start all your Hono routes with /api

Now you are ready to install Hono and the Azure Functions Adapter with:

sh
npm i @marplex/hono-azurefunc-adapter hono
sh
yarn add @marplex/hono-azurefunc-adapter hono
sh
pnpm add @marplex/hono-azurefunc-adapter hono
sh
bun add @marplex/hono-azurefunc-adapter hono

3. Hello World

Create src/app.ts:

ts
// src/app.ts
import { Hono } from 'hono'
const app = new Hono()

app.get('/', (c) => c.text('Hello Azure Functions!'))

export default app

Create src/functions/httpTrigger.ts:

ts
// src/functions/httpTrigger.ts
import { app } from '@azure/functions'
import { azureHonoHandler } from '@marplex/hono-azurefunc-adapter'
import honoApp from '../app'

app.http('httpTrigger', {
  methods: [
    //Add all your supported HTTP methods here
    'GET',
    'POST',
    'DELETE',
    'PUT',
  ],
  authLevel: 'anonymous',
  route: '{*proxy}',
  handler: azureHonoHandler(honoApp.fetch),
})

4. Run

Run the development server locally. Then, access http://localhost:7071 in your Web browser.

sh
npm run start
sh
yarn start
sh
pnpm start
sh
bun run start

5. Deploy

INFO

Before you can deploy to Azure, you need to create some resources in your cloud infrastructure. Please visit the Microsoft documentation on Create supporting Azure resources for your function

Build the project for deployment:

sh
npm run build
sh
yarn build
sh
pnpm build
sh
bun run build

Deploy your project to the function app in Azure Cloud. Replace <YourFunctionAppName> with the name of your app.

sh
func azure functionapp publish <YourFunctionAppName>

Released under the MIT License.