EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis API

Redis API

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

REDIS API

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

Redis API Database

Redis API Server

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:

Redis API URL

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:

Giving token request

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:

Redis API - Redis CLI

Executing Redis API command

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:

post json or a binary value

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:

final parameter

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:

request body and a single JSON array

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:

Redis API Pipelining

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:

Redis API Multiple commands

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:

Redis API - Adding Header

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:

Redis API - Performance Optimization

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 –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
Primary Sidebar
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more