Run mtr with HTTP using the Globalping API

Do you need to analyze network performance and identify packet loss from your web app, serverless function, or edge app? While the traditional MTR command may not be available in these environments, we have excellent news for you: you can run MTR with HTTP using the Globalping API.

In this blog post, we'll demonstrate how you can use the Globalping API to run MTR tests with HTTP from any environment where you can make an HTTP request.

The problem: Why you may have issues running MTR

MTR (My Traceroute) is a helpful network diagnostics tool that combines the functionality of two other tools: ping and traceroute. It continuously sends packets to a target and provides real-time information about packet loss, latency, and network performance for each hop along the network path. As a result, MTR is great for finding network problems that don't happen consistently and for spotting performance bottlenecks that you might miss with tools like ping or traceroute, which only test once.

However, there are situations where the traditional MTR command faces limitations or simply won't work:

  • Environmental constraints: MTR requires low-level network access that isn't available in web browsers, serverless functions, or edge applications.
  • Network restrictions: Some networks, cloud environments, and high-security setups block or limit the ICMP packets or raw sockets that MTR depends on. As a result, you may get incomplete results or none at all.
  • Geographical issues: When you run MTR from only one location, you might miss network problems that only happen on specific routes.

So, when you need the ability to analyze network paths but can't use traditional MTR, what options do you have? Running MTR with HTTP through an API could be the perfect solution for you!

The solution: Use Globalping's HTTP API

Globalping is a network measurement platform that uses a global probe network to let you run tests like ping, traceroute, MTR, dig, and curl from virtually any location. Our platform is open-source, comes with high limits, and offers various integrations, including a CLI tool and an MCP server, giving you different options to access the platform's capabilities.

The core of Globalping (and the solution to your MTR problem) is our API. All our integrations and tools use the API, which is also available to you to use directly whenever you need flexible and direct access to create network measurements.

Here's how the Globalping API can help you overcome the challenges described earlier:

  • Environmental accessibility: Since you're running the MTR test via an HTTP request, all the required network operations happen on the Globalping probes instead of your restricted environment.
  • Overcoming network limitations: All traffic comes from the Globalping probes, which means it does not go through your local network and can avoid any local network restrictions that would normally block ICMP traffic.
  • Global testing: Globalping allows you to run MTR tests from multiple locations at once, allowing you to identify region-specific routing problems and compare performance.

How to run MTR with HTTP using Globalping

Let's see the Globalping API in action and how to create your first simple MTR test.

For this, we'll use the "Try" functionality in the API reference and also share the respective curl commands.

Before we start, a couple of points for you to consider:

  • 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 give you higher API limits. Check the Globalping API reference for more information on auth.
  • API limit: Limits apply to the number of tests you run, not the number of API requests. For example, if you run an MTR measurement from three different locations in one API call, that counts as three tests against your limit. Also, if not authenticated, the API limit applies per IP address. This means, for example, if you use the API from client-side JavaScript, each user will have their own API limit.

We'll use these two endpoints:

  • POST /measurements: creates and triggers a new measurement (in our case, an MTR test).
  • GET /measurements/{id}: retrieves the results by providing the measurement ID.

Run a basic MTR test with HTTP

Let's say we want to run MTR against globalping.io from a probe in the US and another in Europe.

Step 1: Create the measurement

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

We provide a JSON request body to the endpoint containing data about the mtr measurement we want to create.

Here's the curl command:

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

A successful response returns the measurement ID:

The endpoint returns the measurment ID, which we'll need in the next step to request the measurement result.

Make sure to copy this ID because we'll need it in the next step to retrieve the MTR results from the API.

Step 2: Retrieve the measurement results

Note that network measurements can take a few seconds to complete. They are ready once the status field in the response is anything other than in-progress. Additionally, MTR tests typically take a bit longer than, for instance, ping tests because they collect information from multiple packets sent to each hop.

Important: Follow the polling best practices in the "Client Guidelines" section of the endpoint to avoid reaching your API limits unnecessarily.

To get the results, provide the measurement ID to the measurement/{id} endpoint:

We use the measurment ID to retrieve the mtr results.

Here's the curl command:

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

If successful, the API returns a JSON payload with MTR results for each probe that ran the command.

Here's what you'll find in the response:

  • results.probe: Contains details about the probe, including its location, network, resolvers, and assigned tags.
  • results.result: Contains the data returned by the MTR test. It includes the raw output along with detailed statistics for each hop, including packet loss, latency measurements (minimum, maximum, average), and standard deviation.

More use cases and examples

Finally, let's explore some examples that show how you can customize your MTR measurements by providing different data to the request body.

Analyze performance from specific cloud providers

Knowing how your application performs over different cloud services is important for improving the user experience of your app or service. Let's say we want to check the network path from an AWS region in the US to our Globalping website:

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

Use the magic field for easier targeting

The magic field allows you to provide location data as a simple string, making probe selection much more straightforward. You can combine multiple locations using the + operator.  For example, instead of typing aws-us-east-1 to target the data center, you can simply write US+AWS.

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

Run multiple tests per location

If you want to get MTR results from multiple probes in the same location, use the limit option:

{
  "target": "globalping.io",
  "type": "mtr",
  "locations": [
    {"country": "JP", "limit": 3},
    {"city": "London", "limit": 2}
  ]
}

Use MTR-specific options

Under measurementOptions, you can define different options that are specific to Globalping's MTR command:

  • packets: Number of packets to send to each hop (default: 3)
  • protocol: Use ICMP, TCP, or UDP as the transport protocol (default: ICMP)
  • port: Port number to use for TCP and UDP (default: 80)
  • ipVersion (experimental): Use IPv4 or IPv6. (default: 4)

In the following example, we want to send 10 packets per hop and use the TCP protocol on port 443:

{
  "target": "globalping.io",
  "type": "mtr",
  "measurementOptions": {
    "packets": 10,
    "protocol": "TCP",
    "port": 443
  },
  "locations": [
    {"magic": "US+AWS"}
  ]
}

Conclusion

We hope this blog post could help you run MTR with HTTP using the Globalping API. Using the API, you can analyze network paths from any environment that supports HTTP requests, whether it's your JavaScript app or serverless function.

Ready to give it a go? While we've worked with the API directly 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" functionality to get started with free MTR tests.