REST API enables you to access your Upstash database using REST.
If you do not have a database already, follow these steps to create one.
In the database details section of the Upstash Console, click the
REST API button.
Copy the REST URL and the authentication token. Send an HTTP GET request to the provided URL by adding an
Authorization: Bearer $TOKEN header.
The above script executes a
SET foo bar command. It will return a JSON response:
You can also set the token as
_token request parameter as below:
Upstash REST API follows the same convention with Redis Protocol. Give the command name and parameters
in the same order as Redis protocol by separating them with a
Here some examples:
SET foo bar->
SET foo bar EX 100->
GET foo bar->
MGET foo1 foo2 foo3->
HGET employee:23381 salary->
ZADD teams 100 team-x 90 team-y->
To post a JSON or a binary value, you can use an HTTP POST request and set value as the request body:
Note that POST request body is appended as the last parameter of a Redis command. If there are other parameters in Redis command after the value, you should send them as request parameters:
200 OK: When request is accepted and successfully executed.
400 Bad Request: When there's a syntax error, an invalid/unsupported command is sent or command execution fails.
401 Unauthorized: When authentication fails; auth token is missing or invalid.
405 Method Not Allowed: When an unsupported HTTP method is used. Only
PUTmethods are allowed.
REST API returns a JSON response. When command execution is successful, response JSON will have a single
and its value will contain the Redis response. It can be either;
- an integer
- a string
- an array value:
If command is rejected or fails, response JSON will have a single
error field with a string value explaining the failure:
We provide a unique access token per database:
You need to add a header to your API requests as
Authorization: Bearer $TOKEN or set the token as a url parameter
Do not expose your
Token publicly. Access token has full privilege over the database.
If your access token is leaked, you can revoke it by resetting password of your database.
In our internal tests comparing the REST API and native Redis API, we see the latency overhead of REST is less than 1 millisecond. But you can still improve latency connecting to the database port directly. The only disadvantage of this approach is that some environments (e.g. CloudFlare Workers) do not allow clients to access ports other than standard ones (80, 443).
You can use the same url with two changes.
- Append the database port to the url.
- If TLS is disabled, use http instead of https.
Optimized Redis URL (TLS enabled):
Optimized Redis URL (TLS disabled):
If you want to access to Upstash database from an environment like CloudFlare Workers, WebAssembly, Fastly Compute@Edge then you can not use Redis protocol as it is based on TCP. You can use REST API in those environments.
REST API is request (HTTP) based where Redis protocol is connection based. If you are running serverless functions (AWS Lambda etc), you may need to manage the Redis client's connections. REST API does not have such an issue.
Redis protocol requires Redis clients. On the other hand, REST API is accessible with any HTTP client.
If you have a legacy code based on Redis clients, Redis protocol will help you to use Upstash without a code change.
Using Redis protocol, you can benefit from the rich Redis ecosystem. For example, you can directly use your Upstash database as session cache for your express application.
REST API has lower latency than GraphQL API. With REST API, you directly access to the database. With GraphQL API, there is a proxy layer which accepts the connections and translate GraphQL queries to the Redis protocol.
If you do not have a specific GraphQL use case, we recommend REST API instead of GraphQL API. We plan to deprecate the GraphQL API in future releases.
Upstash pricing is based on per command/request. So the same pricing listed in our pricing applies to your REST calls too.
In the current version, we do not expose any metrics specific to API calls in the console. But the metrics of the database backing the API should give a good summary about the performance of your APIs.
|List||✅||Blocking commands (BLPOP - BRPOP - BRPOPLPUSH) are not supported.|
|SortedSet||✅||Blocking commands (BZPOPMAX - BZPOPMIN) are not supported.|
|Connection||❌||Only PING and ECHO are supported.|