Run ping with HTTP using the Globalping API

Do you need to run a ping test directly from your web app, a serverless function, or an edge app? While the traditional ping command may not be available in these environments, we have good news for you. In this blog post, we'll show how you can use Globalping's API to easily run ping tests with HTTP from any location where you can make an HTTP request.

The problem: Why you may not be able to ping

ping is a popular tool for quickly checking if a network host, such as a website or API endpoint, is reachable and measuring its round-trip times. The command works by sending small ICMP (Internet Control Message Protocol) "echo requests" and waiting for a reply, providing a quick network health check.

However, depending on your environment, you might not be able to run ping:

  • Firewall restrictions: Your network admin, cloud provider, or even ISP may have configured firewalls to block ICMP traffic. As a result, your ping tests can't get through, even if HTTP requests to the same host are working.
  • Environmental constraints: The environment in which you want to run ping might not support it. This includes client-side JavaScript, serverless functions, and edge applications.

So, what can you do if you really need to run a ping test or enable your app users to do so? The standard ping command may not be an option, but running a ping with HTTP through an API could be a great solution for you.

The solution: Globalping's HTTP API

The Globalping platform relies on a large network of community-hosted probes located around the globe. It enables users to run network commands such as ping, traceroute, mtr, dig, and curl from any location. Globalping is open-source, comes with generous API limits, and offers various integrations, including a CLI tool and a Slack app, providing different ways to access the platform's features.

The core of Globalping, and the solution to your ping problem, is its API. It serves as the foundation for all our integrations and tools, and it's available to you whenever you need flexible and direct access to create network measurements.

Here's how the Globalping API can solve the ping problems we just described:

  • Bypassing ICMP blocks: Since you're triggering the ping test via an HTTP call, the ICMP traffic goes through the Globalping probes and not your local environment.
  • Accessibility: As long as you (or your code) can send an HTTP request, you can use the API to initiate a ping test.

Additionally, Globalping allows you to run ping tests from anywhere in the world, not just your physical location. You can also run multiple tests from different places at once, which isn't easily achieved with traditional ping.

How to run ping with HTTP using Globalping

Let's take a look at the Globalping API in action and how to create your first ping test.

We'll use the "Try" functionality in the API reference and provide you with the respective cURL commands.

Before we start, consider the following:

  • Authentication: You don't need to authenticate to use the API. However, signing up for the Globalping Dashboard and then authenticating your API requests will unlock higher API limits. Please check our Globalping API Reference for authentication details.
  • API limits: Limits apply to the number of tests you run, not the number of API requests. For example, if you run a ping measurement from three different locations, that counts as three tests against your limit. Also, if not authenticated, the API limit applies per IP address. This means if you use the API from client-side JavaScript, each user will have their own API limit.

We will use these two API endpoints:

  1. POST /measurements: This endpoint creates and triggers a new measurement (in our case, a ping test).
  2. GET /measurements/{id}: Once a measurement is created, we'll use this endpoint to retrieve the results by providing the measurement ID.

Triggering a basic ping with HTTP

Let's say we want to ping globalping.io from a probe in the United States and another in Germany.

Step 1: Create the measurement

First, we'll send a POST request to the /measurements endpoint with a JSON body specifying our ping test details:

The respective curl command:

curl -X POST https://api.globalping.io/v1/measurements \
  --header 'Content-Type: application/json' \
  --data '{
    "target": "globalping.io",
    "type": "ping",
    "locations": [
      {"country": "US"},
      {"country": "DE"}
    ]
  }'

A successful request returns a JSON response containing the measurement ID:

Copy this ID because we'll need it in the next step to retrieve the ping results.

Step 2: Retrieve the measurement results

Network measurements can take a few seconds to complete and are only ready once the `status` in the response is anything other than `in-progress`. This means you may need to wait for a short moment.

💡
Important: If you want to programmatically retrieve measurement results, follow our polling best practices, as described in the "Client Guidelines" section of the endpoint, to avoid reaching your API limits unnecessarily.

To retrieve the measurement result, provide its ID to the /measurements/{id} endpoint:

The respective curl command:

curl -X GET https://api.globalping.io/v1/measurements/your-measurement-id-goes-here

A successful request returns a JSON payload with ping results for each probe that ran the command.

Look for information in the following objects:

  • results.probe: contains details about the probe, including its location, network, resolvers, and assigned tags.
  • results.result: contains the data returned by the ping test. It includes the raw output along with `timings` (round-trip times) and `stats`, which contain packet loss, as well as minimum, maximum, and average round-trip times.

More use cases and examples

Here are a few more examples to show how you can customize your ping measurement by providing different data to the request body:

Check reachability from a specific locations

The `locations` object supports different ways to define locations for your ping. For example, let's say you want to ping your website from a probe in the AWS network in the United States:

{
  "target": "globalping.io",
  "type": "ping",
  "locations": [
    {"country": "US", "tags": ["aws-us-east-1"]}
  ]
}

Use the magic field

To make things a bit easier, you can use the magic field, which allows you to provide location data as a single string.

For example, to replicate the previous example using the magic field, you can provide US+AWS as a location, where the + sign indicates that the locations should be combined in a filter.

{
  "target": "globalping.io",
  "type": "ping",
  "locations": [
    {"magic": "US+AWS"},
    {"magic": "AS13335"},
    {"magic": "Google"}
  ]
}

Define limits

If you specify three different locations in your measurement request, the API will pick one probe from each location. Additionally, you can also define to run multiple tests per location using limit:

{
  "target": "globalping.io",
  "type": "ping",
  "locations": [
    {"country": "US", "limit": 2},
    {"city": "Berlin", "limit": 3}
  ]
}

Use ping-specific options

Under measurementOptions, you can define one or more options that are specific to your command.

For ping, you can use packets to define how many packets to send and ipVersion to select whether to use IPv4 or IPv6 (please note that this feature is currently experimental).

{
  "target": "globalping.io",
  "type": "ping"
  "measurementOptions":
    "packets": 6
}
💡
Note: If you don't specify a location, as in this example, the API will randomly select one for you.

Conclusion

We hope this blog post can help you with your need to run ping with HTTP using the Globalping API.

Ready to try it for yourself? While we've made raw API requests in this guide, you can also use our TypeScript library for easier integration, especially in Node.js environments such as edge computing workloads.

Head over to the Globalping API and use the "Try It" functionality to get started with free tests.