Publish to a Queue via HTTP
Publish to a Queue directly via HTTP and Workers.
The following example shows you how to publish messages to a queue from any HTTP client, using a shared secret to securely authenticate the client.
This allows you to write to a Queue from any service or programming language that support HTTP, including Go, Rust, Python or even a Bash script.
- A queue created via the Cloudflare dashboard ↗ or the wrangler CLI.
- A configured producer binding in the Cloudflare dashboard or
wrangler.toml
file.
Configure your wrangler.toml
file as follows:
Before you deploy the Worker, you need to create a secret that you can use as a shared secret. A shared secret is a secret that both the client uses to authenticate and the server (your Worker) matches against for authentication.
To generate a cryptographically secure secret, you can use the openssl
command-line tool and wrangler secret
to create a hex-encoded string that can be used as the shared secret:
Copy this string and paste it into the prompt for wrangler secret
:
This secret will also need to be used by the client application writing to the queue: ensure you store it securely.
The following Worker script:
- Authenticates the client using a shared secret.
- Validates that the payload uses JSON.
- Publishes the payload to the queue.
To deploy this Worker:
To make sure you successfully authenticate and write a message to your queue, use curl
on the command line:
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 403, this is because the
Authorization
header is invalid, or you did not configure a secret. - If you receive a HTTP 500, this is either because you did not correctly create a shared secret to your Worker, or you attempted to send an invalid message to your queue.
You can use wrangler tail
to debug the output of console.log
.