Skip to main content
Version: 22.2

HTTP Proxy API

Redpanda HTTP Proxy (pandaproxy) allows access to your data through a REST API. For example, you can list topics or brokers, get events, produce events, subscribe to events from topics using consumer groups, and commit offsets for a consumer.

Prerequisites​

Start Redpanda​

The first step is to deploy Redpanda, which can be done in multiple ways. See the Install Redpanda guides for more information.

HTTP Proxy is enabled by default on port 8082. To change the proxy port, edit redpanda.yaml:

...
pandaproxy:
pandaproxy_api:
- address: 0.0.0.0
port: 8082
...
note

The rest of this section assumes that the HTTP proxy port is 8082, your container (or pod in Kubernetes) is named redpanda-0, and your namespace is panda-ns (in Kubernetes).

Configure rpk​

Make sure rpk is configured for your Redpanda deployment, so you can use it to create a topic:

alias rpk="docker exec -ti redpanda-0 rpk"

Create a topic​

Create a topic to use with HTTP Proxy:

rpk topic create test_topic -p 3

For more information, see rpk Commands.

Set up libraries​

You need an app that calls the HTTP Proxy endpoint. This app can be curl (or a similar CLI), or it could be your own custom app written in any language. Below are curl, JavaScript and Python examples.

Curl is likely already installed on your system. If not, see curl download instructions.

Access your data​

Here are some sample commands to produce and consume streams:

Get list of topics​

curl -s "localhost:8082/topics"

Expected output:

["test_topic"]

Send events to a topic​

Use POST to send events in the REST endpoint query. The header must include the following line:

Content-Type:application/vnd.kafka.json.v2+json

The following commands show how to send events to test_topic:

curl -s \
-X POST \
"http://localhost:8082/topics/test_topic" \
-H "Content-Type: application/vnd.kafka.json.v2+json" \
-d '{
"records":[
{
"value":"Redpanda",
"partition":0
},
{
"value":"HTTP proxy",
"partition":1
},
{
"value":"Test event",
"partition":2
}
]
}'

Expected output (may be formatted differently depending on the chosen application):

{"offsets":[{"partition":0,"offset":0},{"partition":2,"offset":0},{"partition":1,"offset":0}]}

Get events from a topic​

After events have been sent to the topic, you can retrieve these same events.

curl -s \
"http://localhost:8082/topics/test_topic/partitions/0/records?offset=0&timeout=1000&max_bytes=100000"\
-H "Accept: application/vnd.kafka.json.v2+json"

Expected output:

[{"topic":"test_topic","key":null,"value":"Redpanda","partition":0,"offset":0}]

Consume topic events with consumers​

To retrieve events from a topic using consumers, you must create a consumer and a consumer group, and then subscribe the consumer instance to a topic. Each action involves a different endpoint and method.

The first endpoint is: /consumers/<test_group_name>. For this REST call, the payload is the group information.

curl -s \
-X POST \
"http://localhost:8082/consumers/test_group"\
-H "Content-Type: application/vnd.kafka.v2+json" \
-d '{
"format":"json",
"name":"test_consumer",
"auto.offset.reset":"earliest",
"auto.commit.enable":"false",
"fetch.min.bytes": "1",
"consumer.request.timeout.ms": "10000"
}'

Expected output:

{"instance_id":"test_consumer","base_uri":"http://127.0.0.1:8082/consumers/test_group/instances/test_consumer"}
note

Consumers expire after five minutes of inactivity. To prevent this from happening, try consuming events within a loop. If the consumer has expired, you can create a new one with the same name.

Subscribe to the topic​

After creating the consumer, subscribe to the topic that you created.

curl -s -o /dev/null -w "%{http_code}" \
-X POST \
"http://localhost:8082/consumers/test_group/instances/test_consumer/subscription"\
-H "Content-Type: application/vnd.kafka.v2+json" \
-d '{
"topics": [
"test_topic"
]
}'

Expected response is an HTTP 204, without a body. Now you can get the events from test_topic.

Retrieve events​

Retrieve the events from the topic:

curl -s \
"http://localhost:8082/consumers/test_group/instances/test_consumer/records?timeout=1000&max_bytes=100000"\
-H "Accept: application/vnd.kafka.json.v2+json"

Expected output:

[{"topic":"test_topic","key":null,"value":"Redpanda","partition":0,"offset":0},{"topic":"test_topic","key":null,"value":"HTTP proxy","partition":1,"offset":0},{"topic":"test_topic","key":null,"value":"Test event","partition":2,"offset":0}]

Get offsets from consumer​

curl -s \
-X 'GET' \
'http://localhost:8082/consumers/test_group/instances/test_consumer/offsets' \
-H 'accept: application/vnd.kafka.v2+json' \
-H 'Content-Type: application/vnd.kafka.v2+json' \
-d '{
"partitions": [
{
"topic": "test_topic",
"partition": 0
},
{
"topic": "test_topic",
"partition": 1
},
{
"topic": "test_topic",
"partition": 2
}
]
}'

Expected output:

{ "offsets": [{ "topic": "test_topic", "partition": 0, "offset": 0, "metadata": "" },{ "topic": "test_topic", "partition": 1, "offset": 0, "metadata": "" }, { "topic": "test_topic", "partition": 2, "offset": 0, "metadata": "" }] }

Commit offsets for consumer​

After events have been handled by a consumer, the offsets can be committed, so that the consumer group won't retrieve them again.

curl -s -o /dev/null -w "%{http_code}" \
-X 'POST' \
'http://localhost:8082/consumers/test_group/instances/test_consumer/offsets' \
-H 'accept: application/vnd.kafka.v2+json' \
-H 'Content-Type: application/vnd.kafka.v2+json' \
-d '{
"partitions": [
{
"topic": "test_topic",
"partition": 0,
"offset": 0
},
{
"topic": "test_topic",
"partition": 1,
"offset": 0
},
{
"topic": "test_topic",
"partition": 2,
"offset": 0
}
]
}'

Expected output: none.

Get list of brokers​

curl "http://localhost:8082/brokers"

Expected output:

{brokers: [0]}

Delete a consumer​

To remove a consumer from a group, send a DELETE request as shown below:

curl -s -o /dev/null -w "%{http_code}" \
-X 'DELETE' \
'http://localhost:8082/consumers/test_group/instances/test_consumer' \
-H 'Content-Type: application/vnd.kafka.v2+json'

Use Swagger with HTTP Proxy​

You can use Swagger UI to test and interact with Redpanda HTTP Proxy endpoints.

Use Docker to start Swagger UI:

docker run -p 80:8080 -d swaggerapi/swagger-ui

Verify that the Swagger container is available:

docker ps

Verify that the Docker container has been added and is running:

swaggerapi/swagger-ui with Up… status

In a browser, enter localhost in the address bar to open the Swagger console.

Change the URL to http://<host_address>:8082/v1, and click Explore to update the page with Redpanda HTTP Proxy endpoints. You can call the endpoints in any application and language that supports web interactions. The following examples show how to call the endpoints using Curl, NodeJS, and Python.

What do you like about this doc?




Optional: Share your email address if we can contact you about your feedback.

Let us know what we do well: