Updated February 17, 2023
Introduction to Redis API
Redis API helps us to automate common tasks. Authentication for redis enterprise software API which occurs by using basic auth. At the time of using it, we need to provide a username and password for the basic auth credentials. If suppose username and password are incorrect then the request is failing with an unauthorized status code. By default, the admin user is used to authorize for accessing all the endpoints.
The redis rest API will use JSON like java script object notation for the response and requests. In it, some responses contain an empty body, but it will indicate the response with the standard http code. Both the response and the requests are included in one or more objects. If the request contains a single entity, the response will either return the single JSON object or return none.
If suppose request contains a list of entities, then the response will return a JSON array with zero or multiple elements. If we omit a field of a JSON object from the request, it will be assigned to the default values, indicating that those fields were not in use. Redis uses rest API self-signed certificates for ensuring that the product is secure. When we are using a self-signed certificate for http requests then it will fail with validation of the SSL certificate. All the calls of it are made over the SSL to port 9443.
Key Takeaways
- To use it, we must execute the docker command. All Redis APIs will be versioned in order to minimize the impact of API changes for coordinating different versions of operations.
- We need to specify the version in the request of URI, which was defined in versions.
How to Create Redis API?
It enables to access the database of redis using API. To create it we are creating the redis database on the redis enterprise server. The below image show we are creating the api database on the redis server. After creating the database we also create the user for accessing the database as follows. In the below example, we can see that the public endpoint is created for accessing the database.
Database name – api
Redis version – 6.2.3
Public endpoint – redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com:19071
After creating the database, now we are copying the details of database like the URL and token of authentication for sending the http get request as follows.
Command:
curl https://redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com:19071/set/foo/bar -H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6"
Output:
We can also set the http get by using the following command below. We are directly giving token requests with the following command.
Command:
curl https://redis-19071.c267.us-east-1 4.ec2.cloud.redislabs.com/set/foo/bar?_token=A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6
Output:
It follows the same command with API which we execute with redis-cli. The below example shows how we can execute the command as follows. The below example shows the REST api as follows.
Command:
curl redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/set/foo/bar
curl redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/set/foo/bar/EX/100
Output:
To post json or a binary value we use a post request of http and set value for the request body.
Command:
curl -X POST -d '$VALUE' https://redis-19071.c267.us-east-14.ec2.cloud.redislabs.com/set/foo -H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6"
Output:
In the above example, $VALUE is sent into the request body, which is appended to the command that we provided. The body of the post request is appended as the final parameter in the redis command.
Command:
curl -X POST -d '$VALUE' https://redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/set/foo?EX=100 \ -H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6"
Output:
We can send all of the commands using the request body and a single JSON array. The first name array contains the command name and parameters, which were appended in the same order.
Command:
curl -X POST -d '["SET", "foo", "bar", "EX", 100]' https://redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/set/foo?EX=100 -H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6"
Output:
The HTTP code that we defined using the curl command is shown below:
Curl supports a variety of HTTP codes.
- 200 ok – When the request is accepted successfully, this code returns.
- 400 bad request – When a syntax error occurs, this code returns.
- 401 unauthorized – This code is returned when authentication fails or the authentication token is missing.
- 405 method not allowed – When the http method is used, this code is returned.
The Redis rest API provides a JSON response. When the execution is successful, the JSON response will return a single result. The following are the responses from it. The following example shows pipelining.
- Null value
- Integer
- String
- Array value
Example:
Command:
curl -X POST /redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/pipeline \
-H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6" \
-d '
[
["SET", "API1", "val1"],
["SETEX", "API2", 13, "val2"],
["INCR", "API1"],
["ZADD", "myset", 11, "val3", 22, "val4"]
]
'
Output:
It supports transactions for automatically executing multiple commands. The transactions shown in the example below are as follows.
Command:
curl -X POST /redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/multi-exec \
-H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6" \
-d '
[
["SET", "API1", "val1"],
["SETEX", "API2", 13, "val2"],
["INCR", "API1"],
["ZADD", "myset", 11, "val3", 22, "val4"]
]
'
Output:
We need to add the header for our API requests in redis or we need to set the token as a parameter.
Command:
curl -X POST /redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com/info \
-H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6"
Output:
We are using performance optimization in it. The below example shows performance optimization as follows.
Command:
curl -X POST /redis-19071.c267.us-east-1-4.ec2.cloud.redislabs.com:19071/set/foo/bar \
-H "Authorization: Bearer A4lxhllu169je1oqnszexyyqxryoyvq36ecxm5ywehrf02yxoh6"
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of rest API in redis?
Answer: It enables us to access our redis database by using REST. We can access the database and execute the command by using the curl command.
Q2. What is the http code in redis API?
Answer: HTTP code is nothing but the code which was returned while executing the curl command. Basically, 200, 400, 401, and 405 http codes are available in it.
Q3. What is the use of response in redis API?
Answer: Redis rest API returns the JSON response. The null, integer, string, and array responses are used in it.
Conclusion
The redis rest API will use JSON like java script object notation for the response and requests. In it, some responses contain an empty body, but it will indicate the response with the http code which was standard. It helps us to automate common tasks. Authentication for redis enterprise software API which occurs by using basic auth.
Recommended Articles
This is a guide to Redis API. Here we discuss the introduction, how to create Redis API, and FAQ for better understanding. You may also have a look at the following articles to learn more –