Publish to a Queue via Workers

Publish to a Queue directly from your Worker.

Last updated Apr 21, 2026 The following example shows you how to publish messages to a Queue from a Worker. The example uses a Worker that receives a JSON payload from the request body and writes it as-is to the Queue, but in a real application you might have more logic before you queue a message.

Prerequisites

Configure your Wrangler file as follows:

{
    "$schema": "./node_modules/wrangler/config-schema.json",
    "name": "my-worker",
    "queues": {
        "producers": [
            {
                "queue": "my-queue",
                "binding": "YOUR_QUEUE"
            }
        ]
    }
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "my-worker"

[[queues.producers]]
queue = "my-queue"
binding = "YOUR_QUEUE"

1. Create the Worker

The following Worker script:

  1. Validates that the request body is valid JSON.
  2. Publishes the payload to the queue.
interface Env {
    YOUR_QUEUE: Queue;
}

export default {
    async fetch(req, env, ctx): Promise<Response> {
        // Validate the payload is JSON
        // In a production application, we may more robustly validate the payload
        // against a schema using a library like 'zod'
        let messages;
        try {
            messages = await req.json();
        } catch {
            // Return a HTTP 400 (Bad Request) if the payload isn't JSON
            return Response.json({ error: "payload not valid JSON" }, { status: 400 });
        }

        // Publish to the Queue
        try {
            await env.YOUR_QUEUE.send(messages);
        } catch (e) {
            const message = e instanceof Error ? e.message : "Unknown error";
            console.error(`failed to send to the queue: ${message}`);
            // Return a HTTP 500 (Internal Error) if our publish operation fails
            return Response.json({ error: message }, { status: 500 });
        }

        // Return a HTTP 200 if the send succeeded!
        return Response.json({ success: true });
    },
} satisfies ExportedHandler<Env>;

To deploy this Worker:

npx wrangler deploy

2. Send a test message

To make sure you successfully write a message to your queue, use curl on the command line:

# Make sure to replace the placeholder with your shared secret
curl -XPOST "https://YOUR_WORKER.YOUR_ACCOUNT.workers.dev" --data '{"messages": [{"msg":"hello world"}]}'
{"success":true}

This will issue a HTTP POST request, and if successful, return a HTTP 200 with a success: true response body.

  • If you receive a HTTP 400, this is because you attempted to send malformed JSON to your queue.
  • If you receive a HTTP 500, this is because the message was not written to your Queue successfully.

You can use wrangler tail to debug the output of console.log.

Was this helpful?

YesNo

On this page

Publish to a Queue via Workers - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/queues/examples/publish-to-a-queue-via-workers/#page","headline":"Publish to a Queue via Workers","description":"Publish to a Queue directly from your Worker.","url":"https://developers.cloudflare.com/queues/examples/publish-to-a-queue-via-workers/","inLanguage":"en","image":"https://developers.cloudflare.com/queues/examples/publish-to-a-queue-via-workers/og.png?v=2c014551474492a3","dateModified":"2026-04-21","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Example of how to use Queues to batch data and store it in an R2 bucket. title: Use Queues to store data in R2 image: https://developers.cloudflare.com/queues/examples/send-errors-to-r2/og.png?v=98a9a079242979ec


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/queues/llms.txt
Use this file to discover all available pages before exploring further.