Cloudflare Docs
Workers
Edit this page on GitHub
Set theme to dark (⇧+D)

Upstash

Upstash is a serverless database with Redis* and Kafka API. Upstash also offers QStash, a task queue/scheduler designed for the serverless.

Database Integrations allow you to connect to a database from your Worker by getting the right configuration from your database provider and adding it as secrets to your Worker.

​​ Upstash for Redis

To set up an integration with Upstash:

  1. You need an existing Upstash database to connect to. Create an Upstash database or load data from an existing database to Upstash.

  2. Insert some data to your Upstash database. You can add data to your Upstash database in two ways:

    ➜ set GB "Ey up?"
    OK
    ➜ set US "Yo, what’s up?"
    OK
    ➜ set NL "Hoi, hoe gaat het?"
    OK
  3. Add the Upstash Redis integration to your Worker:

    1. Log in to the Cloudflare dashboard and select your account.
    2. In Account Home, select Workers & Pages.
    3. In Overview, select your Worker.
    4. Select Settings > Integrations > Upstash Redis.
    5. Follow the setup flow, selecting the database created in step 1.
  4. In your Worker, install the @upstash/redis, a HTTP client to connect to your database and start manipulating data:

    $ npm install @upstash/redis
  5. The following example shows how to make a query to your Upstash database in a Worker. The credentials needed to connect to Upstash have been automatically added as secrets to your Worker through the integration.

Worker code
import { Redis } from "@upstash/redis/cloudflare";
export default {
async fetch(request, env) {
const redis = Redis.fromEnv(env);
const country = request.headers.get("cf-ipcountry");
if (country) {
const greeting = await redis.get(country);
if (greeting) {
return new Response(greeting);
}
}
return new Response("Hello What's up!");
},
};

To learn more about Upstash, refer to the Upstash documentation.

​​ Upstash Kafka

To set up an integration with Upstash Kafka:

  1. Create a Kafka cluster and topic.

  2. Add the Upstash Kafka integration to your Worker:

    1. Log in to the Cloudflare dashboard and select your account.
    2. In Account Home, select Workers & Pages.
    3. In Overview, select your Worker.
    4. Select Settings > Integrations > Upstash Kafka.
    5. Follow the setup flow, selecting the cluster and topic.
  3. In your Worker, install @upstash/kafka, a HTTP/REST based Kafka client:

    $ npm install @upstash/kafka
  4. Use the upstash-kafka JavaScript SDK to send data to Kafka.

Refer to Upstash documentation on Kafka setup with Workers for more information. Replace url, username and password with the variables set by the integration.

​​ Upstash QStash

To set up an integration with Upstash QStash:

  1. Configure the publicly available HTTP endpoint that you want to send your messages to.

  2. Add the Upstash QStash integration to your Worker:

    1. Log in to the Cloudflare dashboard and select your account.
    2. In Account Home, select Workers & Pages.
    3. In Overview, select your Worker.
    4. Select Integrations > Upstash QStash.
    5. Follow the setup flow.
  3. In your Worker, install the @upstash/qstash, a HTTP client to connect to your database QStash endpoint:

    $ npm install @upstash/qstash
  4. Refer to the Upstash documentation on how to receive webhooks from QStash in your Cloudflare Worker.

* Redis is a trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Any use by Upstash is for referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and Upstash.