ReferenceError: fetch is not defined

Problem

If you are running on nodejs v17 and earlier, fetch will not be natively supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill for you. But if you are running on bare node, you need to add a polyfill.

Solution

npm i isomorphic-fetch
import { Redis } from "@upstash/redis";
import "isomorphic-fetch";

const redis = new Redis({
  /*...*/
});

Hashed Response

The response from a server is not what you expect but looks like a hash?

await redis.set("key", "value");
const data = await redis.get("key");
console.log(data);

// dmFsdWU=

Problem

By default @upstash/redis will request responses from the server to be base64 encoded. This is to prevent issues with some edge cases when storing data where the http response fails to be deserialized using res.json()

This solves the problem for almost all edge cases, but it can cause new issues.

Solution

You can disable this behavior by setting responseEncoding to false in the options.

const redis = new Redis({
  // ...
  responseEncoding: false,
});

This should no longer be necessary, but if you are still experiencing issues with this, please let us know:

Large numbers are returned as string

You are trying to load a large number and it is returned as a string instead.

await redis.set("key", "101600000000150081467");
const res = await redis("get");

// "101600000000150081467"

Problem

Javascript can not handle numbers larger than 2^53 -1 safely and would return wrong results when trying to deserialize them. In these cases the default deserializer will return them as string instead. This might cause a mismatch with your custom types.

Solution

Please be aware that this is a limitation of javascript and take special care when handling large numbers.