Application Insights REST API

The Application Insights REST API lets you query the performance and usage data that Application Insights records about your app. To get started, follow these steps.

1. Explore the API

Here are some examples of common queries the Metrics, Events & Query APIs can be used for. Click the Try it. buttons to try them out in this site's API Explorer.

API Scenario Query
Metrics: Total number of requests in the last day (timespan=P1D) GET /v1/apps/{app-id}/metrics/requests/count?timespan=P1D
Try it
Metrics: Average server response time per hour (interval=PT1H) for the last 6 hours GET /v1/apps/{app-id}/metrics/requests/duration?timespan=PT6H&interval=PT1H
Try it
Events: List the last 5 events GET /v1/apps/{app-id}/events/$all?$top=5
Try it
Events: List GET requests which failed or took longer than 0.5 seconds GET /v1/apps/{app-id}/events/requests?$filter=startswith(request/name, 'GET') and (request/resultCode ne '200' or request/duration gt 500)
Try it
Query: Total number of requests in the last 24 hours GET /v1/apps/{app-id}/query?query=requests | where timestamp >= ago(24h) | count
Try it
Query: Percentile (50th & 95th) response time for the last hour GET /v1/apps/{app-id}/query?query=requests | where timestamp >= ago(1h) | summarize percentiles(duration, 50, 95) Try it

Enjoy experimenting with the queries - this is the best way to learn the API. This site's API Explorer allows you to try out any request you want either with a demo application or with your own app that you've instrumented wtih Application Insights. You can learn more about the syntax for the query path.

2. Get an API key for your app

Now that you've tried out the API with a demo app above, to access your own app's telemetry data, you need the Application ID and an API key.

Get them from the Application Insights resource for your application.

Click Settings, API Key

Notice that you must take a copy of the API Key at the time you create it. (If you lose it, you can delete it and create another.) By contrast, the Application Id doesn't change, and you can go back and get it at any time. Notice that the Application Id is not the same as the instrumentation key.

Test your API Key

Use API Explorer, providing it with your application ID and API key.

3. Make calls against the API

The Public API has the format:

https://api.applicationinsights.io/{version}/apps/{app-id}/{operation}/[path]?[parameters]
X-API-Key:{key}
  1. version: the API version to call; for now this will always be beta.
  2. app-id: the Application ID that you got from the portal.
  3. operation: the supported API operations are metrics, events and query.
  4. path: for operations that require it, the URL path is used to specify which metric or event types to query.
  5. parameters: for operations that require it, URL query string parameters specify how and what to query.
  6. key In addition, you must provide the API key that you got from the Azure portal in a custom header X-API-Key.

Call the API with cURL

One way to access the API is to the cURL utility. You can pipe the output into jsonlint to get readable JSON.

Note that the API Explorer provides the cURL command as an additional tab in the Request section in the top right of the page.

Example: Return the total number of requests in the last 24 hours:

curl "https://api.applicationinsights.io/v1/apps/0MyAppId0/metrics/requests/count"
-H "X-Api-Key: 0MyApiKey0"

Here are some more samples showing how to access the /metrics path of the API programatically:

C#

private const string URL =
        "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";

public static string GetTelemetry(string appid, string apikey,
        string queryType, string queryPath, string parameterString)
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add("x-api-key", apikey);
    var req = string.Format(URL, appid, queryType, queryPath, parameterString);
    HttpResponseMessage response = client.GetAsync(req).Result;
    if (response.IsSuccessStatusCode)
    {
        return response.Content.ReadAsStringAsync().Result;
    }
    else
    {
        return response.ReasonPhrase;
    }
}

...

var json = GetTelemetry
    ("DEMO_APP",  "DEMO_KEY",
        "metrics", "requests/duration", "interval=PT1H");

JQuery

$.ajax({
        url: 'https://api.applicationinsights.io/v1/apps/DEMO_APP/metrics/requests/duration?interval=PT1H',
        headers: { "x-api-key": "DEMO_KEY" },
        datatype: 'json',
        success: function (data) { doSomething(data); }
});

JavaScript

hr = new XMLHttpRequest();
hr.onreadystatechange = function() {
    if (hr.readyState == XMLHttpRequest.DONE) {
    doSomething(hr.responseText);
}}
hr.open('GET', 'https://api.applicationinsights.io/v1/apps/DEMO_APP/metrics/requests/duration?interval=PT1H', true);
hr.setRequestHeader("x-api-key", "DEMO_KEY");
hr.send();

4. Keep going!

Read through the scenario-based documentation on Using the API and the API reference.

Learn about alternative authentication methods.

Learn about accessing your app's information using the Azure API format and its support for authentication using Azure Active Directory.

5. Let us know what you think

Questions or support? Send us your questions at StackOverflow using the tag ms-application-insights or at the Application Insights MSDN forum.

Ideas for new features? You can submit ideas or requests for new features at the Application Insights UserVoice site.