Pub/Sub quickstart
This quickstart guides you through creating a topic, attaching a subscription, publishing a message, pulling the message, acknowledging it, and cleaning up resources.
Prerequisites
Before starting:
- Log in to your Crusoe AI Platform account using
platformctl login. - Ensure you have administrator access to your target project to create topics and subscriptions.
- For
curlcommands, set your environment variables:
export CAI_PUBSUB_API="https://api.codyhill.dev"
export CAI_PROJECT="<your-project-id>"
export CAI_TOKEN="<your-api-key-or-session-token>"
Step 1: Create a topic
A topic is a named channel for your messages. This command creates a topic named orders with a 16 MiB storage allocation and an old discard policy when capacity is reached.
- platformctl
- curl
- Console UI
platformctl pubsub topics create orders --max-bytes 16Mi --discard old
curl -sX POST "$CAI_PUBSUB_API/v1/projects/$CAI_PROJECT/topics" \
-H "Authorization: Bearer $CAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"orders","max_bytes":"16Mi","discard":"old"}'
Expected response (201 Created):
{
"name": "orders",
"max_bytes": "16Mi",
"discard": "old",
"ready": true,
"state": "ready"
}
- Open the Crusoe Console and select your project.
- Navigate to Messaging → Pub/Sub.
- Click Create Topic.
- Enter
ordersas the name, set the max storage budget to16Mi, selectoldfor the discard policy, and click Create.
Step 2: Create a subscription
Subscriptions receive and retain messages published to a topic. You must create a subscription before publishing messages to ensure they are retained.
- platformctl
- curl
- Console UI
platformctl pubsub subscriptions create workers --topic orders \
--type shared --ack-deadline-seconds 30 --start-from all
curl -sX POST "$CAI_PUBSUB_API/v1/projects/$CAI_PROJECT/topics/orders/subscriptions" \
-H "Authorization: Bearer $CAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"workers","type":"shared","ack_deadline_seconds":30,"start_from":"all"}'
Expected response (201 Created):
{
"name": "workers",
"topic": "orders",
"type": "shared",
"ack_deadline_seconds": 30,
"deliver": {"mode": "pull"},
"ready": true,
"state": "ready"
}
- Under Pub/Sub → Topics, select the
orderstopic. - Click Create Subscription.
- Name the subscription
workers, set the type toshared, and set the ack deadline to30seconds. - Click Save.
Step 3: Publish a message
Publish a test message to the orders topic.
- platformctl
- curl
- Console UI
platformctl pubsub topics publish orders --message "hello world" --attribute region=eu
Output:
1234:0
curl -sX POST "$CAI_PUBSUB_API/v1/projects/$CAI_PROJECT/topics/orders:publish" \
-H "Authorization: Bearer $CAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{"messages":[{"text":"hello world","attributes":{"region":"eu"}}]}'
Expected response:
{
"message_ids": ["1234:0"]
}
- Open the
orderstopic details page. - Click Publish Message.
- Enter
hello worldin the message body field, add an attributeregion=eu, and click Send.
Step 4: Pull and acknowledge the message
Pull waiting messages from the subscription and acknowledge processing.
- platformctl
- curl
- Console UI
platformctl pubsub subscriptions pull workers --topic orders --max 10 --ack
Output:
ACK_ID ID KEY DATA
a1b2c3d4 1234:0 - hello world
acknowledged 1 message(s)
Pull with auto_ack enabled to fetch and confirm in one call:
curl -sX POST "$CAI_PUBSUB_API/v1/projects/$CAI_PROJECT/topics/orders/subscriptions/workers:pull" \
-H "Authorization: Bearer $CAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{"max_messages":10,"auto_ack":true}'
Expected response:
{
"messages": [
{
"id": "1234:0",
"data": "aGVsbG8gd29ybGQ=",
"attributes": {"region": "eu"},
"delivery_attempt": 1
}
],
"acknowledged": 1
}
- Navigate to the
workerssubscription. - Click Pull Messages.
- View the retrieved message and click Acknowledge to complete processing.
Step 5: Check storage quota
View your project's storage usage and budget limits.
- platformctl
- curl
- Console UI
platformctl pubsub quota
curl -s "$CAI_PUBSUB_API/v1/projects/$CAI_PROJECT/pubsub/quota" \
-H "Authorization: Bearer $CAI_TOKEN"
Check the Pub/Sub header dashboard to see claimed storage allocation vs available capacity.
Publishing from a deployed workload
The commands above authenticate as you, against the public API host. A function or agent running on the platform does neither, and copying them into a handler fails in a way that points at the wrong thing.
- Address: use the injected
CAI_PUBSUB_URLwithCAI_PROJECT_ID. Read the variables rather than hard-coding what they hold — that private address is the platform's to change. The public hosthttps://api.codyhill.devdoes not route from inside a project — a handler that reaches for it hangs until its own timeout. - Credential: use a service-account key with the
memberrole, stored as a project secret and bound to the workload.memberis enough to publish; creating topics and subscriptions needsadmin. - Not the injected
CAI_PROJECT_KEY. It holds no project authority — its one power is minting a token to read this project's secrets — and it is refused on every project route with404 not found, not403. So a wrong credential looks exactly like a misspelled topic name. Check the credential before you go hunting for the topic.
import json, os, urllib.request
PUBSUB = os.environ['CAI_PUBSUB_URL'].rstrip('/')
PROJECT = os.environ['CAI_PROJECT_ID']
KEY = os.environ['PIPELINE_KEY'] # the bound service-account key
req = urllib.request.Request(
'%s/v1/projects/%s/topics/orders:publish' % (PUBSUB, PROJECT),
data=json.dumps({'messages': [{'text': 'order-created'}]}).encode('utf-8'),
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer ' + KEY},
method='POST')
urllib.request.urlopen(req, timeout=30)
Bind the secret, then Apply. A handler that reads the key at import time fails its first revision every time, because a bound secret only reaches the workload on the revision created after the Apply.
Cleanup
Delete the topic when finished testing. Deleting a topic automatically removes associated subscriptions.
- platformctl
- curl
- Console UI
platformctl pubsub topics delete orders
curl -sX DELETE "$CAI_PUBSUB_API/v1/projects/$CAI_PROJECT/topics/orders" \
-H "Authorization: Bearer $CAI_TOKEN"
Select the orders topic, click Delete, and confirm the deletion prompt.
Next steps
- Topics and subscriptions — Learn about message retention, quota allocation, and ordering models.
- Publish and consume — Advanced push and pull configurations, CloudEvent integration, and the full workload-side publishing walkthrough.