Loading...

5 Ways to Host Your API on Internet

5 Ways to Host Your API on Internet

What is an API?

First things first, what’s an API as per definition API stands for Application Programming Interface, and its 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 terms, it’s a link between the backend and front-end code for your website.

api request and response

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. 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.

 

1. From your local computer

The first way to host your API is directly from your local computer. Even though this sounds weird, it’s pretty easy and for 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 is 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.

 

2. AWS Lambda

Most of the cloud providers provide some kind of serverless functions to host your API, which is offered as FaaS (Functions as a Service) so that you can 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 you upload your code to AWS, they give an endpoint. When some send a request to the endpoint AWS creates a server and runs the code and responds with the appropriate data and when sees that there is no oncoming traffic, the server shuts down. Hence saving a lot of money for the user. The serverless setup is much cheaper, scalable, and much more convenient than hosting it on your local machine.

aws lambda

3. Virtual Machine (EC2)

The third way to host your computer online is using a virtual computer, in the case of AWS it’s EC2, it’s a computer that would run 24 / 7. For setting this up, let assume that you have already created the 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.

amazon ec2

Now that your script is in the virtual machine the only thing left 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 on. Now you have fulfilled all the requirements to access the API. It’s advisable to mask the IP address on a domain name so that it’s less prone to attacks.

4. Cloudflare Workers

cloudflare

The fourth way to host your API is to use Cloudflare Workers, there are very similar to Amazon Lambda functions, the only difference here is that instead of setting it up in a single location, Cloudflare deploys them in multiple locations and they will be running the script inside a browser environment which is supported by some web assembly code which is written in C++ or Rust or any other Web Assembly compatible language. 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.

5. fly.io

When coming to the last one to host your API on the list it’s fly.io, which is also similar to Cloudflare workers, but has additional benefits, as Cloudflare workers run in the browser environment, as developers we can’t use native libraries on the runtime, now with the help of fly.io, our code is building 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.

Watch the full YouTube video here

Sharing is caring

Did you like what Pranav wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: