In this blog post we will teach you how to test an API running on localhost from outside localhost. This is useful if you are in the process of developing an API and want to interact with it from Apibot.
Before we get started you will need two things:
- Ngrok (download free)
- A working REST API running on localhost
What is ngrok?
Ngrok is a free command line tool that allows you to expose a service running on your local machine to the internet. Make sure you download and put ngrok somewhere in your path e.g. /usr/local/bin/ngrok
.
Run your web service
For the purpose of this example I will be running a simple Rails app that I found somewhere in the internet. If you want to follow along with this tutorial but don’t know what to run, you can run something simple like an HTTP echo server.
After having installed ngrok
, run the following command to expose your server to the internet:
ngrok http <port number>
Replace <port number>
with the port number where your server is running. After running the ngrok
command you will notice that your terminal screen looks something like this:
Tunnel Status online
Update update available (version 2.2.8, Ctrl-U to update)
Version 2.0.25/2.1.18
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://8ddae8ce.ngrok.io -> localhost:3000
Forwarding https://8ddae8ce.ngrok.io -> localhost:3000
Take note of the https://*.ngrok.io
URL. Whenever you make an HTTP request to this URL it will be redirected to your localhost.
The ngrok UI
You will also notice that ngrok is displaying another URL
Web Interface http://127.0.0.1:4040
Open that link on your browser. This is a realtime mini-dashboard that displays useful information about your ngrok
connection as well as a history of HTTP requests sent to ngrok
.
Now that we have everything setup, let’s start testing our API.
Testing the API
Now that we have an API running and exposed via ngrok
to the web, we can use Apibot to test some of its endpoints. For the purpose of this blog post we will create a simple graph which sends an HTTP request to authenticate with the API.
Step #1: Creating a graph
Start Apibot, authenticate and then click on the ‘create a new graph’ button. This will create an empty graph. Graphs in Apibot are analogous to “tests” or “workflows”.
Step #2: The Config
We now need to create a config
node. The config
node makes it possible to define variables which can later be accessed from other nodes.
Add a config
node to the graph and enter the following property:
key: root
, value: https://<subdomain>.ngrok.io
Make sure you replace <subdomain>
with your ngrok
subdomain.
Step #3: The HTTP request
Add an HTTP request and fill in the required arguments. This will of course depend on the API you are communicating with.
The most important thing to notice here is that in the url
field I entered ${root}/login
. When a graph is executed, ${root}
will be replaced with whatever you defined the root
variable to be, which in our case is the ngrok
URL.
Step #4: Running the graph
Click the run
button. This will send the HTTP request to the ngrok
URL. If you open the ngrok
UI (normally running on http://127.0.0.1:4040) you will see your request.
Conclusion
This blog post showed you how to expose an HTTP service running on localhost to the internet and then how to interact with it using Apibot.