Let's Learn, Build and Sell An API - by Rakesh Potnuru - CodeX - Apr, 2022 - Medium
Let's Learn, Build and Sell An API - by Rakesh Potnuru - CodeX - Apr, 2022 - Medium
Published in CodeX
Save
If you are in tech, then you may have heard this popular term called “API”. Some
people use APIs for fun, some for money and some for their applications. There are N
ways you can use APIs. In this blog, let’s learn what exactly is an API, how you can
build your own API and how you can monetize your API.
What is an API?
I am taking a popular example to explain this. Imagine you go to a restaurant to eat
some food. Now you don’t directly go to the kitchen and cook yourself and then eat it,
right 😂? (Of course, they don’t allow you to do so). You call a waiter and order your
food. Then the waiter goes to the kitchen
419
and brings
5 your food.
Get unlimited access Open in app
Here you can compare API with the waiter. So, API is an intermediary between two
applications and makes it possible for those two applications to communicate with
each other. If we put this in our example, one application is you the customer, another
application is the restaurant kitchen where the food is prepared and the waiter is an
API who acts as an intermediary between you and the kitchen.
Wh d d API ?
Why do we need APIs?
Get unlimited access Open in app
Imagine you have data and you want to share that data to allow developers to build
software with your data. Now you need some sort of way you can make this possible.
That’s where APIs can help you. You can build an API to share your data and other
resources so that developers can use your API to build services or software. Let’s
understand this with an example, Let’s say you are building an app that suggests the
vehicle take the route with less traffic. For this, you need traffic data of different routes
so that you can train a machine learning model and build your app. It’s not an easy task
to count the number of vehicles travelling on different routes and prepare data. So
what you can do is, use a 3rd party service that provides their data with APIs.
Before building our own API let’s use an API. We will be using a JokeAPI.
Endpoint — An endpoint is an API server URL where you can access all the different
resources that API provides. Endpoints are actions like GET , POST , DELETE , etc.., which
and so on…
For example:
https://api.github.com/user - is a path/route
Parameter — All the paths are pre-defined in the API server. If you have a path that
can’t be pre-defined in the server, then you can use parameters. Parameters are key-
value pairs and start after ? from the end of a path.
For example,
For example,
https://api.github.com/user?userId=12335235235&api_key=yu67432ffu6f2t446
https://v2.jokeapi.dev/joke/Any
This is called a “response” you got from JokeAPI for your request. And the format of the
response is “JSON”. JSON is a popular output format for APIs.
If you visit JokeAPI documentation, you can try out different categories and filters.
Get unlimited
In the above options, each category is a different route/path, like access Open in app
https://v2.jokeapi.dev/joke/Programming
https://v2.jokeapi.dev/joke/Miscellaneous
https://v2.jokeapi.dev/joke/Dark
and all the options below category can be appended as parameters, like
https://v2.jokeapi.dev/joke/Programming?
blacklistFlags=nsfw&type=twopart&amount=2
After tweaking the options, copy the URL and paste it into the browser,
Get unlimited access Open in app
Now you will get a response with all the filters applied.
2. API service — An API service gives people access to their data through API. For
example, JokeAPi, The Movie Database, Open Weather API, etc.
Let’s build an API service to add, delete, edit and get your daily tasks. We need a
, , g y y
database and a server to create an API service. Let’s useGet
MongoDB as our database and
unlimited access Open in app
Open your IDE or code editor. Create a folder and name it something like todo-
api .
Before we start make sure you have these dev tools installed,
NodeJs
MongoDB
npm init
Install express , mongoose , and axios packages as we use them for the project.
Install nodemon as a dev dependency. (Nodemon restarts the server every time we
make changes to the code so that we don't need to manually restart)
todo-api/package.json
"scripts": {
...
...
},
Next, create a file called server.js in the root and paste this boilerplate code.
Get unlimited access Open in app
todo-api/server.js (Snippet)
app.use(express.json());
mongoose
.then(() => {
})
.catch((err) => {
console.log(err);
});
You should see this in your browser. What it is telling you is there is no endpoint like
GET htt //l lh t 5000/ defined in the server
GET http://localhost:5000/ defined in the server.
Get unlimited access Open in app
So let’s add an endpoint. As we are using expressjs we can register a route like this.
todo-api/server.js (Snippet)
res.send("Hello World!");
});
Now visit the URL again and you will see the response.
todo-api/models/tasks.model.js (Snippet)
name: {
type: String,
required: true,
},
});
Before we move further, it’s not possible to do everything from the browser so let’s use
an API tool called “Postman”. Download it from here (Free). After downloading, test it
by entering the URL http://localhost:5000/ and clicking Send.
todo-api/server.js (Snippet)
// GET http://localhost:5000/getTasks
try {
res.json(response);
} catch (err) {
});
If you test this now you will get an empty response as we have not added any tasks to
our database.
So let’s create a route to add tasks to our database. To send data in our request we
need to make a “POST” request.
need to make a POST request.
Get unlimited access Open in app
todo-api/server.js (Snippet)
// POST http://localhost:5000/postTask
try {
res.json(response);
} catch (err) {
});
Now in postman change the GET request to POST . Then go to the Body tab and select
Write a JSON object in the body field and make a POST request to
http://localhost:5000/postTask .
Get unlimited access Open in app
You will receive a response back containing name - the name of the task, _id - the
unique id of the task generated by MongoDB.
Add a few more tasks and make a GET request to http://localhost:5000/ , you will
todo-api/server.js (Snippet)
// DELETE http://localhost:5000/deleteTask/:id
try {
res.json(response);
} catch (err) {
});
Get unlimited access Open in app
So, change the request method to DELETE in postman and copy one of your tasks id and
paste in the path variable value and click Send.
If you now make a GET request to /getTasks you won't see the deleted task. That
means you successfully deleted the Task.
Now let’s edit a task. We all make mistakes so we need an edit button(I Hope Elon
Musk adds an edit button to Twitter). To edit data we have to make a PATCH
request. Let's create a route for that. You can make use PUT request to edit a
document. But PATCH request is better if we want to edit partial data.
todo-api/server.js (Snippet)
// PATCH http://localhost:5000/editTask/:id
try {
res.json(response);
} catch (err) {
});
});
Get unlimited access Open in app
Same as the POST request, add body to your PATCH request. Copy the id of the task that
you wish to edit and paste it into the path variable value field and click Send. Now
make a GET request to /getTasks you will see Task updated.
So that’s it! We learned 4 important RESTAPI methods while building our small “todo
application”.
Rapid API is the world’s largest API hub where you can explore different APIs, and
create and manage your own APIs.
Before continuing, host your API server somewhere like Heroku because you know
Get unlimited access Open in app
“localhost” doesn’t work outside of your computer :). And replace all
http://localhost:5000/ with https://yourdomain.com/ in your postman collection.
Fill in the details for API Name, Short Description and Category. For Specify
using, select “Postman Collection”. And then upload the collection file.
Get unlimited access Open in app
You can download your postman collection by exporting the collection as a JSON file.
To do so, open your postman collection, and click three dots -> Export.
Export Get unlimited access Open in app
Or you can download the JSON file from this tutorial GitHub repository. Make sure to
change the domain name.
After uploading the file and clicking Add API. Fill in the details for “Describe and
click on Save.
Get unlimited access Open in app
Finally, make your API public, so every on the internet can see your API. If they like
it they can subscribe to your API.
Get unlimited access Open in app
Let’s actually monetize our API by adding Plans & Pricing. So go to the Plans &
Pricing tab.
Here you can choose different plans and set the number of requests for different
plans.
Get unlimited access Open in app
Let’s add a PRO plan. Choose “Montly Subscription” or “Pay Per Use”. Set a price.
Choose rate limit — number of requests per second/minute/hour.
That’s it! I hope you learned something new from this article. Feel free to ask any
questions or doubts or anything in the comments.
Follow me for more stuff like this. Also if you want to receive my articles directly to
your Inbox, subscribe to my Medium newsletter. Thank you.
A weekly newsletter on what's going on around the tech and programming space Take a look.