Skip to Content
Runlane
Recipes

Trigger tasks from Hono

Accept work in a Hono route, then execute it with Postgres polling or serverless AWS Lambda workers.

Use Hono as the producer: the HTTP route calls trigger() and returns 202 with the run ID. A separate worker executes the task. The same route works in a long-running Node.js app and a Node.js Lambda function.

You need Node.js 22.18+, reachable Postgres, and a TypeScript project. Install the packages:

npm install hono @hono/node-server @runlane/core @runlane/lane-postgres-polling zod

Install @runlane/cli as a development dependency. Set DATABASE_URL before loading the runtime. This example returns a greeting so you can verify the whole path without an external API.

Define the task

src/tasks/greet.ts
import { task } from '@runlane/core'
import * as z from 'zod'

export const greet = task({
  id: 'greet',
  schema: z.object({ name: z.string().min(1) }),
  output: z.string(),
  run: ({ name }) => `Hello, ${name}!`,
})

Share one Postgres polling runtime

src/runlane.ts
import { createRunlane, queue } from '@runlane/core'
import { postgresPollingLane } from '@runlane/lane-postgres-polling'
import * as z from 'zod'

import { greet } from './tasks/greet.js'

const { DATABASE_URL } = z.object({ DATABASE_URL: z.string().min(1) }).parse(process.env)

export const runlane = createRunlane({
  lane: postgresPollingLane({ connectionString: DATABASE_URL, schema: 'runlane' }),
  queues: [queue({ name: 'default', default: true })],
  tasks: { greet },
})

The app, worker, and CLI import this module. Keep runtime creation outside the request handler.

For timer-triggered serverless workers, use a scheduled drain with this polling runtime and set runHints: false. Start latency follows the timer interval; each run must fit within the function timeout.

Return an accepted run

src/app.ts
import { Hono } from 'hono'
import * as z from 'zod'

import { runlane } from './runlane.js'

const app = new Hono()
const requestSchema = z.object({ name: z.string().min(1) })

app.post('/greetings', async (c) => {
  const body: unknown = await c.req.json().catch(() => null)
  const input = requestSchema.safeParse(body)
  if (!input.success) return c.json({ error: 'A name is required.' }, 400)

  const { run } = await runlane.trigger(runlane.tasks.greet, input.data)
  return c.json({ runId: run.id }, 202)
})

export default app

202 means Runlane accepted the run, not that the task finished. Keep your normal authentication and rate limits on this route. Hono's error handler returns a generic server error if triggering fails; do not send raw Runlane errors to clients.

For a long-running Node.js server:

src/server.ts
import { serve } from '@hono/node-server'

import app from './app.js'
import { runlane } from './runlane.js'

const server = serve({ fetch: app.fetch, port: 3000 })
process.once('SIGTERM', () => {
  server.close(() => {
    void runlane.close()
  })
})

Run the worker and maintenance

Create the CLI config:

runlane.config.ts
import { type RunlaneCliConfig } from '@runlane/cli'

import { runlane } from './src/runlane.js'

export default { runtime: () => runlane } satisfies RunlaneCliConfig

Apply migrations as a deployment step before starting the app and worker:

npx runlane adapter postgres migrate

Run the polling worker as its own long-running process. This command starts maintenance by default:

npx runlane work

Start the app with your project's TypeScript runner or compiled src/server.ts. Send a request, then read the accepted run:

curl -i http://localhost:3000/greetings \
  -H 'content-type: application/json' \
  -d '{"name":"Ada"}'
npx runlane runs get <run-id> --json

Expect HTTP 202 with runId, then a stored run with status: "succeeded" and output: "Hello, Ada!". Replace <run-id> with the response value. See Postgres polling for deployment and shutdown details.

Run Hono on Lambda as a producer

Keep src/app.ts and the task unchanged. Replace src/runlane.ts with the Postgres + SQS runtime below. Install the additional packages:

npm install @aws-sdk/client-sqs @runlane/lane-postgres-sqs @runlane/transport-sqs

Set DATABASE_URL and RUNLANE_SQS_QUEUE_URL, and configure AWS region and credentials through the AWS SDK's normal settings.

src/runlane.ts
import { SQSClient } from '@aws-sdk/client-sqs'
import { createRunlane, queue } from '@runlane/core'
import { postgresSqsLane } from '@runlane/lane-postgres-sqs'
import { sqsQueue } from '@runlane/transport-sqs'
import * as z from 'zod'

import { greet } from './tasks/greet.js'

const env = z
  .object({
    DATABASE_URL: z.string().min(1),
    RUNLANE_SQS_QUEUE_URL: z.url(),
  })
  .parse(process.env)
const defaultQueue = queue({ name: 'default', default: true })

export const runlane = createRunlane({
  lane: postgresSqsLane({
    runHints: false,
    postgres: { connectionString: env.DATABASE_URL, schema: 'runlane' },
    sqs: {
      client: new SQSClient({}),
      queues: [sqsQueue(defaultQueue, { queueUrl: env.RUNLANE_SQS_QUEUE_URL })],
    },
  }),
  queues: [defaultQueue],
  tasks: { greet },
})

Export Hono's AWS adapter as the HTTP Lambda entry point:

src/lambda/http.ts
import { handle } from 'hono/aws-lambda'

import app from '../app.js'

export const handler = handle(app)

The HTTP Lambda only accepts work. Configure a separate SQS-triggered Lambda to execute it:

src/lambda/delivery.ts
import { runlane } from '../runlane.js'

export const handler = runlane.createDeliveryHandler()

Configure scheduled maintenance as another Lambda:

src/lambda/maintenance.ts
import { runlane } from '../runlane.js'

export const handler = runlane.createMaintenanceHandler()

Follow Deploy to AWS to provision Postgres, SQS, the event source mapping with partial batch responses, and the recurring maintenance invocation. Use the same runtime configuration for all three functions. Apply migrations before deployment. Keep tasks within Lambda's execution budget, or run a long-running SQS consumer for longer work. Reuse the runtime across warm invocations.

Show live status

Use the returned run ID to set up realtime. That guide includes a Hono handler mount and authorization. React hooks can show status, output, and saved streams while the task runs.

On this page