Pub/sub - API - REST

Publishing messages and getting them from queues over REST - endpoints, parameters and samples.

Overview

If you're new to the publish/subscribe pattern, start with What is publish/subscribe?

Before making use of the REST pub/sub API, a REST endpoint needs to be created with HTTP Basic Auth credentials and appropriate permissions.

REST integration with publish/subscribe is built around three operations:

OperationREST
PublishingPOST /pubsub/topic/{topic_name}
Getting messages from queuesPOST /pubsub/messages/get
Acknowledging messagesPOST /pubsub/messages/ack
  • To make use of any of these endpoints, a REST client must use HTTP Basic Auth credentials previously assigned to it by Zato admins

  • REST clients can subscribe to topics if they have proper subscription permissions. Subscribing to the same topic multiple times is safe and has no effect. All messages from all subscribed topics are delivered to a single queue per user.

    • REST clients receive their messages in one of two ways:
      • Pull-style: clients call /pubsub/messages/get to retrieve messages on demand
      • Push-style: Zato delivers messages to configured REST endpoints as soon as they arrive

OpenAPI definition

  • In your Dashboard at http://localhost:8183, click EDA → Download OpenAPI to download the complete specification of the interface, for instance, to generate API clients from it, or to use it with tools such as Postman

  • You can also download the specification from GitHub

Demo credentials, topics and subscriptions

  • In your Dashboard at http://localhost:8183, click EDA → Import test config to auto-configure demo credentials, and a few topics, so that you can quickly try out the REST API

  • The demo credentials are user.1/password.1

  • Note that the test config does not survive container restarts and it's meant to be used during development and testing, for your convenience, but the configuration is real, your actual credentials or topics will look similarly

Publishing messages

Topic name must be provided in the URL path. Message data and optional parameters are given as JSON payload.

VerbURL
POST/pubsub/topic/{topic_name}

Request

ParameterDatatypeRequiredNotes
dataanyYesActual data of the message that is being published. Can be a string or a JSON object.
priorityinteger---Message priority from 0 to 9 (0=lowest, 9=highest). Defaults to 5 if not given.
expirationinteger---Expiration in seconds. Defaults to 31536000 (1 year) if not given.
correl_idstring---Correlation ID for message tracking. Defaults to the request correlation ID if omitted.
in_reply_tostring---ID of message this is replying to.
ext_client_idstring---External client identifier for logging and audit purposes.
pub_timestring---Custom publication time in ISO format with timezone (e.g., "2025-01-01T12:00:00+00:00"). Server sets to current time if not provided.
msg_idstring---Message ID of the client's own, 1 to 200 characters, each a letter, a digit or one of ., _, : and -. A new ID is generated if not given.

Response

ParameterDatatypeRequiredNotes
is_okbooleanYesWhether the operation succeeded
cidstringYesCorrelation ID for request tracking
msg_idstringYesUnique message ID
statusstringYesHTTP status description (e.g., "200 OK")

Samples

OK, data sent and message ID returned:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/topic/orders.processed \
  -d '{"data":"Order #12345 has been processed", "priority": 7}'
{
  "is_ok": true,
  "cid": "correlation-id-123",
  "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc",
  "status": "200 OK"
}
$

Error, no data sent on input:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/topic/orders.processed \
  -d '{}'
{
  "is_ok": false,
  "cid": "correlation-id-456",
  "details": "Invalid request data",
  "status": "400 Bad Request"
}
$

Publishing the same message again

A client that did not receive the response to a publication, for instance because the connection was lost, does not know whether the message was stored. When the client gives each message a msg_id of its own, it can send the same request again without creating a second message:

  1. A request whose msg_id is not stored yet stores the message and returns its msg_id.
  2. A request with the same msg_id, the same topic and the same data stores nothing and returns 200 OK with the same msg_id.
  3. A request with the same msg_id and a different topic or different data stores nothing and returns 409 Conflict.

A msg_id is recognized for as long as its message is kept. A message is kept until it expires, and a message that every subscriber received is kept for 7 days afterwards, or for the number of days set through the Zato_PubSub_Delivered_Max_Days environment variable of the server.

OK, the same message published twice, and stored once:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/topic/orders.processed \
  -d '{"data":"Order #12345 has been processed", "msg_id": "order-12345.processed"}'
{
  "is_ok": true,
  "cid": "correlation-id-201",
  "msg_id": "order-12345.processed",
  "status": "200 OK"
}
$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/topic/orders.processed \
  -d '{"data":"Order #12345 has been processed", "msg_id": "order-12345.processed"}'
{
  "is_ok": true,
  "cid": "correlation-id-202",
  "msg_id": "order-12345.processed",
  "status": "200 OK"
}
$

Error, the message ID belongs to a message with different data:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/topic/orders.processed \
  -d '{"data":"Order #12345 has been cancelled", "msg_id": "order-12345.processed"}'
{
  "is_ok": false,
  "cid": "correlation-id-203",
  "details": "Message ID `order-12345.processed` belongs to a different message that exists already",
  "status": "409 Conflict"
}
$

Getting messages from queues

Messages from all subscribed topics are retrieved through a single endpoint. Optional parameters control the number and size of messages returned.

Each retrieved message stays in the queue until the client acknowledges it through /pubsub/messages/ack:

  1. A call to /pubsub/messages/get hands the messages out to this client and reserves them for 30 seconds, during which no other call receives them.
  2. The client processes the messages and sends their msg_id values to /pubsub/messages/ack, which removes them from the queue.
  3. A message that was not acknowledged within the 30 seconds is handed out again by the next call to /pubsub/messages/get. A response that never reached the client, for instance because the connection was lost, therefore loses no messages.

A client that processes the same message twice with the same result needs no further precautions. A client whose processing consists of publishing other messages can acknowledge each message in the same transaction as its publications, as described in Acknowledging messages with publications, and each message then results in its publications exactly once. Any other client records the msg_id of each processed message and skips a message whose msg_id it already processed. The reservation period is set through the Zato_PubSub_Pull_Lease_Seconds environment variable of the server.

VerbURL
POST/pubsub/messages/get

Request

ParameterDatatypeRequiredNotes
max_messagesinteger---Maximum number of messages to retrieve. Defaults to 50, maximum is 1000.
max_leninteger---Maximum total length of message data in bytes. Defaults to 20000000 (20 MB), which is also the maximum value. A message larger than this is returned on its own.

Response

ParameterDatatypeRequiredNotes
is_okbooleanYesWhether the operation succeeded
cidstringYesCorrelation ID for request tracking
messagesarray---Array of messages (possibly empty if none were enqueued)
dataany---Message data (present for single message response)
metaobject---Message metadata (present for single message response)
message_countintegerYesNumber of messages returned
statusstringYesHTTP status description (e.g., "200 OK")

Message metadata fields

FieldDatatypeNotes
topic_namestringTopic the message was published to
sizeintegerMessage size in bytes
priorityintegerMessage priority (0-9)
expirationintegerMessage expiration time in seconds
msg_idstringUnique message identifier
delivery_attemptintegerHow many times the message was retrieved, 1 for the first retrieval and higher for each repeated one
correl_idstringCorrelation ID for message tracking
pub_time_isostringWhen the message was published (ISO format with timezone)
recv_time_isostringWhen the message was received by the system (ISO format with timezone)
expiration_time_isostringWhen the message will expire (ISO format with timezone)
time_since_pubstringTime elapsed since message was published (duration format)
time_since_recvstringTime elapsed since message was received (duration format)
ext_client_idstringExternal client identifier (optional)
in_reply_tostringID of message this is replying to (optional)
sub_keystringSubscription key for the receiving user

Samples

OK, multiple messages returned:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/get \
  -d '{"max_messages": 5}'
{
  "is_ok": true,
  "cid": "correlation-id-789",
  "messages": [
    {
      "data": {
        "order_id": 12345,
        "status": "completed"
      },
      "meta": {
        "topic_name": "orders.processed",
        "size": 45,
        "priority": 5,
        "expiration": 3600,
        "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc",
        "delivery_attempt": 1,
        "correl_id": "order-12345",
        "pub_time_iso": "2025-01-01T12:00:00+00:00",
        "recv_time_iso": "2025-01-01T12:00:01+00:00",
        "expiration_time_iso": "2025-01-01T13:00:00+00:00",
        "time_since_pub": "0:00:30.123456",
        "time_since_recv": "0:00:30.123456"
      }
    }
  ],
  "message_count": 1,
  "status": "200 OK"
}
$

OK, no messages available:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/get
{
  "is_ok": true,
  "cid": "correlation-id-101",
  "messages": [],
  "message_count": 0,
  "status": "200 OK"
}
$

Error, no subscription found for user:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/get
{
  "is_ok": false,
  "cid": "correlation-id-102",
  "details": "No subscription found for user",
  "status": "401 Unauthorized"
}
$

Acknowledging messages

Messages retrieved from a queue are removed from it when the client acknowledges them, either by their msg_id values alone or by their msg_id values together with the delivery_attempt of each, which also lets the client publish messages in the same transaction.

VerbURL
POST/pubsub/messages/ack

Request

A request has either msg_ids or consumed, never both.

ParameterDatatypeRequiredNotes
msg_idsarray---The msg_id values of the messages to acknowledge, at most 1000 of them.
consumedarray---The messages to acknowledge, at most 1000 of them, each an object with the msg_id of the message and the delivery_attempt with which it was retrieved.
publisharray---Messages to publish in the same transaction, at most 1000 of them, permitted only with consumed. Each is an object with topic_name and the parameters of Publishing messages.

Response

ParameterDatatypeRequiredNotes
is_okbooleanYesWhether the operation succeeded
cidstringYesCorrelation ID for request tracking
ack_countinteger---How many of the messages were still in the queue and were removed from it
publishedarray---The msg_id of each message of publish, in the same order, returned for a request with consumed
statusstringYesHTTP status description (e.g., "200 OK")

Samples

OK, both messages acknowledged:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/ack \
  -d '{"msg_ids": ["zpsm.20250829-074432-7264-d47439a9f-abc", "zpsm.20250829-074433-1182-a03b5c71e-abc"]}'
{
  "is_ok": true,
  "cid": "correlation-id-103",
  "ack_count": 2,
  "status": "200 OK"
}
$

OK, the messages were already acknowledged earlier:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/ack \
  -d '{"msg_ids": ["zpsm.20250829-074432-7264-d47439a9f-abc"]}'
{
  "is_ok": true,
  "cid": "correlation-id-104",
  "ack_count": 0,
  "status": "200 OK"
}
$

Acknowledging messages with publications

A client that reads orders from its queue and publishes an invoice for each one can acknowledge an order and publish its invoice in one transaction. Either both are stored or neither is, so a crash of the client or a lost connection never leaves an invoice without its acknowledged order, or an acknowledged order without its invoice.

The full cycle consists of four requests:

  1. A publisher sends the order to the orders topic.

    $ curl -XPOST http://publisher.1:password.1@localhost:11223/pubsub/topic/orders \
      -d '{"data": {"order_id": 12345}}'
    {
      "is_ok": true,
      "cid": "correlation-id-301",
      "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc",
      "status": "200 OK"
    }
    
  2. The client subscribed to orders retrieves the order, together with its msg_id and delivery_attempt.

    $ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/get
    {
      "is_ok": true,
      "cid": "correlation-id-302",
      "messages": [
        {
          "data": {"order_id": 12345},
          "meta": {
            "topic_name": "orders",
            "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc",
            "delivery_attempt": 1
          }
        }
      ],
      "message_count": 1,
      "status": "200 OK"
    }
    
  3. The client acknowledges the order with the delivery_attempt it received and publishes the invoice in the same request. The msg_id of the invoice is derived from the msg_id of the order, which makes the request safe to repeat.

    $ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/ack \
      -d '{
        "consumed": [{"msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc", "delivery_attempt": 1}],
        "publish": [{
          "topic_name": "invoices",
          "data": {"order_id": 12345, "amount": 100},
          "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc.invoice"
        }]
      }'
    {
      "is_ok": true,
      "cid": "correlation-id-303",
      "ack_count": 1,
      "published": ["zpsm.20250829-074432-7264-d47439a9f-abc.invoice"],
      "status": "200 OK"
    }
    
  4. The client subscribed to invoices retrieves the invoice.

    $ curl -XPOST http://user.2:password.2@localhost:11223/pubsub/messages/get
    {
      "is_ok": true,
      "cid": "correlation-id-304",
      "messages": [
        {
          "data": {"order_id": 12345, "amount": 100},
          "meta": {
            "topic_name": "invoices",
            "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc.invoice",
            "delivery_attempt": 1
          }
        }
      ],
      "message_count": 1,
      "status": "200 OK"
    }
    

The outcome of the third request depends on the state of the order when the request arrives:

  1. The order is in the queue with the given delivery_attempt. The order is removed and the invoice is stored, and the response is 200 OK.
  2. The order was already acknowledged by the same request, whose response did not reach the client. Nothing is stored again, and the response is 200 OK with the same published list, because the invoice already exists under its msg_id.
  3. The order was not acknowledged within the reservation period and was retrieved again, so its delivery_attempt is higher than the one in the request. Nothing is stored, and the response is 409 Conflict. The client discards its result and processes the order from the latest retrieval.
  4. The order was already acknowledged with a different invoice. Nothing is stored, and the response is 409 Conflict.

Error, the order was retrieved again after the reservation period:

$ curl -XPOST http://user.1:password.1@localhost:11223/pubsub/messages/ack \
  -d '{
    "consumed": [{"msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc", "delivery_attempt": 1}],
    "publish": [{
      "topic_name": "invoices",
      "data": {"order_id": 12345, "amount": 100},
      "msg_id": "zpsm.20250829-074432-7264-d47439a9f-abc.invoice"
    }]
  }'
{
  "is_ok": false,
  "cid": "correlation-id-305",
  "details": "Message `zpsm.20250829-074432-7264-d47439a9f-abc` was handed out again with delivery attempt 2, the acknowledgement is for attempt 1",
  "status": "409 Conflict"
}
$

The client needs the permission to publish to each topic of publish, and each of these topics needs to be active.

Push delivery

A push subscription delivers each message to its REST endpoint as an HTTP POST request. A response with a 2xx status is a delivery, and the message is removed from the queue.

Any other response is a failed attempt. Zato repeats the message according to the retry policy of the subscription, and once the retries run out, it gives the message up and records it in the audit log.

Push redirects

A response with the status 307 Temporary Redirect or 308 Permanent Redirect is followed, and Zato sends the same request, with the same method, body and headers, to the address in its Location header. Every other redirect status, such as 301, 302 or 303, is a failed attempt.

Zato follows a redirect only when each of these conditions is met:

  • The Location header is present, its scheme is http or https, it contains no user name or password, and it names a host and a port from 1 to 65535. A relative address is resolved against the address of the request that was redirected.
  • The redirect does not lead from https to http.
  • The attempt has been redirected fewer than 5 times.
  • The address of the redirect is in the same network range as the endpoint of the subscription. An endpoint on a public address redirects only to public addresses, an endpoint on a private address, such as 10.0.0.0/8 or fc00::/7, only to private addresses, and an endpoint on 127.0.0.1 only to the loopback range.
  • The endpoint of the subscription is not on a link-local address, such as 169.254.0.0/16 or fe80::/10.

A redirect that does not meet these conditions is a failed attempt. A redirect to another host or another port is followed, and the request sent to it has no Authorization header.

Each repeated attempt starts again at the configured address of the subscription. A 308 Permanent Redirect does not change the configuration of the subscription, so to stop the redirects, update the address of the endpoint in the subscription.

Each followed redirect is logged at the INFO level with its status, the address that was redirected and the address of the redirect. The log line of a 308 states that the endpoint of the subscription moved permanently.

Data format

  • All requests and responses use JSON format
  • Authentication is always with HTTP Basic Auth
  • All responses include an is_ok boolean field indicating success or failure
  • All responses include a cid field for correlation tracking
  • Error responses include a details field with error description
  • Success responses include a status field with HTTP status description

Success response example:

{
  "is_ok": true,
  "cid": "correlation-id-123",
  "status": "200 OK"
}

Error response example:

{
  "is_ok": false,
  "cid": "correlation-id-456",
  "details": "Authentication failed",
  "status": "401 Unauthorized"
}

Topic name restrictions

  • Maximum length: 200 characters
  • Only ASCII characters are permitted
  • The "#" character is not allowed
  • Topic names are case-insensitive

Getting started with EDA

Using EDA