Creating a publicly accessible REST Web API on a Raspberry Pi3

One of the easiest ways to expose your Raspberry Pi publicly is by using ngrok. Ngrok is a lightweight alternative to VPN that creates introspected tunnels to localhost. If you have been following my articles, you will know that I managed to built a REST Web API to check the status of my Raspberry Pi Grid. The project works on my local network and I also have an Android app also that checks if the Grid is alive or not. But the point of building all this is that I should be able to check the status of it anywhere just to make sure that the system is up and running. The easiest way I could find without having to forward ports on my router is by just running Ngrok. 


Why do you need to expose your Raspberry Pi?


Get the latest version of Ngrok for ARM here and unzip the content of it on your local computer (in my case a Windows 10 machine). Then transfer the unzipped file to your Raspberry Pi using your preferred method (in my case I use WinSCP).

Now that the file is in the Raspberry Pi, we need to give it running permissions using the chmod command (I assume here that you know how to SSH to your RPi using putty):

Now we just need to run the ngrok command: ./ngrok http 3000 and all our http traffic on port 3000 will be automatically forwarded:

Notice that now, Ngrok is giving you a public URL that points to your local Raspberry Pi. This means that Ngrok is providing you with a publicly accessible IP address and domain name to your localhost:

Now that Ngrok is running, I need to run my Web API Service on that port 3000. To do that, I will just launch another session to my Raspberry Pi and start my node.js:

Notice that now I can browse that URL (http:/61184bba.ngrok.io/) and check the status of my Raspberri Pis over the internet:
Notice that every time your stop/start ngrok, a new URL is being generated. As you are exposing your data over the internet you need to make sure that this URL is just known by yourself and that you add extra security on your API if required (Now the Raspberry Pi is able to take requests from anyone on the web!).

Using this approach you can publish your applications easily so other can see them while developing them and you can even set up a home server to share data, etc. Possibilities are endless here.

If you check my mobile application (created with my Delphi 10.1 Berlin update 2) you will see that I just need to point the RestClient component to the exposed URL and voila! we'll be able to access that service over the internet:


All the source code is available here:
  • Mobile application (Delphi 10.1 Berlin update 2).
  • Node.js server (Web Api).
Here is the application in action:

Let me know if you have any questions.
Jordi

Comments

  1. The article provides valuable insights into alternatives for the deprecated Google Finance API. It highlights various options like Alpha Vantage, Yahoo Finance, and IEX Cloud, which offer robust data for developers. The content effectively addresses the need for reliable stock market data sources in the absence of the Google Finance API. It's a helpful resource for anyone seeking to integrate stock market information into their projects while staying updated with the latest API solutions. Find more details on Python Google finance API.

    ReplyDelete
  2. Leverage your Raspberry Pi 3 to establish a publicly accessible REST Web API. Utilize its capabilities to host endpoints that offer data or services, enabling seamless communication over the internet. By configuring network settings, ensuring security measures, and deploying a lightweight web server, you can provide external access to your API. This opens doors for remote interactions, IoT applications, and data sharing, expanding the functionality of your Raspberry Pi beyond local use.

    ReplyDelete

Post a Comment

Popular Posts