Queue runtime API

Reference for defineQueue, runQueue, deferQueue, getQueue, and the core Queue types.

Use this page when you need the Queue surface area, signatures, or option names. For a guided first run, start with Quickstart.

Definition APIs

defineQueue(handler, options?)

Use defineQueue() to register one queue definition from the discovery directory.

Queue definitions live in server/queues/**.

import { defineQueue } from '@vitehub/queue'

export default defineQueue(async (job) => {
  return {
    id: job.id,
  }
}, {
  cache: false,
})

createQueue(options?)(handler)

createQueue() is not the normal Nitro or Nuxt definition pattern. Use defineQueue() there.

Enqueue APIs

runQueue(name, input)

Use runQueue() for the normal enqueue path.

You can pass either:

  • a bare payload for the common case
  • an input object when you need delivery options
import { runQueue } from '@vitehub/queue'

await runQueue('welcome-email', {
  email: 'ava@example.com',
})

await runQueue('welcome-email', {
  id: 'welcome-1',
  payload: { email: 'ava@example.com' },
  delaySeconds: 10,
})

deferQueue(name, input)

Use deferQueue() when the send should happen after the current response is already committed.

deferQueue() reads the active request context. It uses waitUntil() when the runtime provides it and falls back to fire-and-forget dispatch otherwise.

import { deferQueue } from '@vitehub/queue'

export default defineEventHandler(() => {
  deferQueue('welcome-email', {
    email: 'ava@example.com',
  })

  return { ok: true }
})

Use Defer after response when you need the request-context rules in more detail.

Enqueue input options

These options are available when you pass an object to runQueue() or deferQueue():

OptionDescription
payloadThe message payload passed to the queue handler.
idOverride the generated job identifier.
maxAttemptsRequest retry attempts when the provider supports them.
resultTTLKeep job results for a limited time when the provider supports it.
timeoutSet an execution or wait timeout when the provider supports it.
idempotencyKeyDeduplicate sends when the provider supports it.
retentionSecondsKeep the queued message for a limited time when the provider supports it.
delaySecondsDelay delivery by a number of seconds.
delayUntilDelay delivery until a specific time when the provider supports it.
priorityRequest provider-specific priority handling.
contentTypeOverride how the payload is encoded when the provider supports it.
Option support varies by provider. Use the provider pages for exact behavior.

Queue handle API

getQueue(name?)

Use getQueue() when you need the active queue handle instead of a send call.

import { getQueue } from '@vitehub/queue'

const queue = await getQueue('welcome-email')

console.log(queue.provider)

Most apps only need runQueue(). Reach for getQueue() when you need provider-native methods such as batch handlers, polling helpers, or direct inspection tools.

Use Native handles for guidance on when to cross that boundary.

Validation helpers

Queue also exports validation helpers for enqueue routes and handlers:

HelperUse it for
readValidatedPayload()Validate user input before enqueueing.
readValidatedJob()Validate the payload inside the queue handler.

Use Validate payloads for examples.

Core types

QueueJob<TPayload>

Queue handlers receive a QueueJob<TPayload> object.

FieldTypeDescription
idstringUnique job identifier.
payloadTPayloadThe payload you enqueued.
attemptsnumberThe current attempt count.
signalAbortSignalAbort signal tied to the job lifecycle.

QueueDefinitionOptions

QueueDefinitionOptions combines portable queue behavior with provider-specific overrides.

OptionDescription
cacheEnable or disable client caching for this queue definition.
concurrencyLimit concurrent processing when the provider supports it.
onErrorHandle queue message failures.
callbackOptionsPer-queue Vercel callback settings.
consumerOverride the Vercel consumer name.
configPass Netlify Async Workloads handler config.
destinationOverride the Upstash QStash callback destination.