Skip to content
Cloudflare Docs

Upstash

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

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:

    • Use the CLI directly from your Upstash console.
    • Alternatively, install redis-cli locally and run the following commands.
    Terminal window
    set GB "Ey up?"
    OK
    Terminal window
    set US "Yo, what’s up?"
    OK
    Terminal window
    set NL "Hoi, hoe gaat het?"
    OK
  3. Configure the Upstash Redis credentials in your Worker:

    You need to add your Upstash Redis database URL and token as secrets to your Worker. Get these from your Upstash Console under your database details, then add them as secrets using Wrangler:

    Terminal window
    # Add the Upstash Redis URL as a secret
    npx wrangler secret put UPSTASH_REDIS_REST_URL
    # When prompted, paste your Upstash Redis REST URL
    # Add the Upstash Redis token as a secret
    npx wrangler secret put UPSTASH_REDIS_REST_TOKEN
    # When prompted, paste your Upstash Redis REST token
  4. In your Worker, install the @upstash/redis, a HTTP client to connect to your database and start manipulating data:

    Terminal window
    npm i @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 added as secrets to your Worker.

    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. Configure the Upstash Kafka credentials in your Worker:

    You need to add your Upstash Kafka connection details as secrets to your Worker. Get these from your Upstash Console under your Kafka cluster details, then add them as secrets using Wrangler:

    Terminal window
    # Add the Upstash Kafka URL as a secret
    npx wrangler secret put UPSTASH_KAFKA_REST_URL
    # When prompted, paste your Upstash Kafka REST URL
    # Add the Upstash Kafka username as a secret
    npx wrangler secret put UPSTASH_KAFKA_REST_USERNAME
    # When prompted, paste your Upstash Kafka username
    # Add the Upstash Kafka password as a secret
    npx wrangler secret put UPSTASH_KAFKA_REST_PASSWORD
    # When prompted, paste your Upstash Kafka password
  3. In your Worker, install @upstash/kafka, a HTTP/REST based Kafka client:

    Terminal window
    npm i @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. Configure the Upstash QStash credentials in your Worker:

    You need to add your Upstash QStash token as a secret to your Worker. Get your token from your Upstash Console under QStash settings, then add it as a secret using Wrangler:

    Terminal window
    # Add the QStash token as a secret
    npx wrangler secret put QSTASH_TOKEN
    # When prompted, paste your QStash token
  3. In your Worker, install the @upstash/qstash, a HTTP client to connect to your database QStash endpoint:

    Terminal window
    npm i @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.