Generally in the field of Information and Technology and web development, we often listen to terms like domain, hosting, services and API. These are technical terms often used by any developers around you. Among them, API, Rest API, host an API on Internet and related terms are used. Today we are going to see about API and ways to host your API.
What does an API mean?
First things first, API stands for Application Programming Interface, and it’s a set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application, or other services. In layman’s terms, it’s a link between the backend and front-end code for your website. It’s the way from which the front side of our website or application gets the data and access to data from different databases or operating systems or other applications.
How can I understand the working of an API?
As if you are in the field of development or IT or programming, then you may basically understand how it works. Its the bridge between backend and frontend systems. To illustrate, here I am supposing the website. So for the web the API contacts through HTTP requests through our website endpoints which are basically a URL like “
example.com/api/users.” So the API could fetch some data from the backend and serve it to your application or can perform some side effects like making changes to the existing data on the database.
Ways to host an API on the Internet
Assuming that you know how API works and you can build APIs. Now it’s time to discuss the platforms that help you host your API so that everyone on the internet can access it. There are a few ways that we can host an API on the Internet and they are:
1. Using your local computer
In the development process of different websites or web applications, we use our local PC or computer as a host where we are assigned with IP address. It seems weird when it comes to API hosting on the internet using your PC, but it is pretty easy and free. While you are developing your API using express or any other library you host it in your “localhost:3000 ” where 3000 is the port through which the network request happens. For hosting your API on the Internet you need an IP Address and port to be assigned. You can use ngrock library, ngrock tunnels your localhost port to a public port so that you can host it online. It is one of the easiest ways to host an API on the Internet.
2. AWS Lambda
AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. So basically AWS Lambda is a cloud service. Such like AWS, most cloud service providers provide some kind of serverless function to host an API, which is offered as a FaaS which means Function as a Service which generalizes that you are using a certain function as a service. AWS Lambda is a great service for building and hosting REST APIs. It offers 1 million (yes, 1,000,000) free API requests/calls per month.
To host your API, you should deploy your code as Lambdas so that they are highly scalable as well in any language that you are using for your backend. Now after uploading code in AWS, you get an endpoint, which is used to make requests. When someone sends a request to the endpoint AWS creates a server and runs the code and responds with the appropriate data if there is no oncoming traffic, the server shuts down. Thus AWS lambda is a cheaper, safer, scalable, and more convenient way.
3. Cloudflare Workers
Another way to host an API on the Internet is using Cloudflare Workers. This is almost similar to AWS Lambda services with very few differences. The difference is that AWS deploys code in just a single location. In contrast, Cloudflare deploys them into multiple locations and runs scripts inside a browser environment that is supported by web assemblies in compatible languages like Rust or C++. The benefit is that when deployed in a browser we get 0 millisecond boot time, so the response from the API here would be much faster than compared to the Lambda functions.
4. Virtual Machine (EC2)
A Virtual Machine (VM) is a compute resource that uses software instead of a physical computer to run programs and deploy apps. Virtual machine technology is used for many use cases across on-premises and cloud environments. You can visualize virtual machine as real computers but exists nowhere in the thin air like we couldn’t see servers in cloud service platforms. The EC2 means that it is the name under which AWS operates for virtual computing. You are using EC2 which means you are using a service from Amazon.
The processing of hosting an API starts by creating an EC2 instance from your account. Now you have to SSH into your EC2 instance from your computer to send the code to the EC2 instance. By this process, your script is now in the virtual machine and the only left thing is to run the script. In this case, we don’t use the native runtime, we might be using pm2, tmux, or screen to run our script so that it can run seamlessly without any interruptions. Now that the script starts running for you to access the API, you’ll have to use the virtual machine’s IP Address with the port on which the script is running. Now you have fulfilled all the requirements to access the API. It is better to mask the IP address on a domain name so that it becomes safer.
5. Using website services
There are many websites on the internet which provides the service to host your API. Most of them are cloud based and similar to Cloudflare workers. Using these service providers, our code is built and is executed to the nearest location to the user in a docker container so that now you can get the benefit of using native libraries in your API as well. These websites are fly.io, ZOAPI.io etc.