Getting Started with API Tests

Getting Started with API Tests

Create an API test

API tests help you monitor your API endpoints and alert you when they are failing or become too slow. These tests verify that your applications are responding to requests and meet any conditions you define, such as response time, HTTP status code, and header or body contents.

In this example, an API test is created to ensure your website is constantly up and providing responses in a specific amount of time.

Configure the request

  1. In the Datadog application, hover over UX Monitoring in the left hand menu and select Synthetic Test.

  2. In the top right corner, click the New Test button.

  3. Select API test.

  4. Define the configuration of your API test:

    • Add the URL of the endpoint you want to monitor. If you don’t know what to start with, you can use https://www.shopist.io/, which is a test web application.
    • Select Advanced Options to use custom request headers, authentication credentials, body content, or cookies.
    • You can set tags such as env:prod and app:shopist on your test. Tags help to keep things organized and allow you to quickly find the tests you’re interested in on the homepage.
    • Choose from the Managed Locations to run your test from.
    • Click the Test URL button.

Define your alert conditions

After clicking Test URL, basic assertions based on your endpoint’s response are automatically populated. Assertions define the alert condition and can be customized. In this example, three default assertions are populated when testing the URL:

These assertions define the alert condition and can be customized. To add a custom assertion, directly click on the response preview or click the New Assertion button to add an assertion manually (e.g., body contains Shop.ist)

Browser test failure

You can set up an alerting condition in order to only be alerted if your endpoint goes down for three minutes, on two different locations:

An alert is triggered if your test fails for 3 minutes from any 2 of 13 locations

You can also decide after how many failures you want a location to be considered as fail. Retries will be run immediately after the location fails. You can configure the test using the option below:

Retry x time before location is marked as failed

Note: By default, there is a 300ms wait before retrying a test that failed. This interval can be configured via the API.

Once alert conditions are set, create a message for the alert and specify what services and team members receive the alert notification email and click Save Test. You can also use integrations, such as Slack, PagerDuty, webhooks, etc., to receive alert notifications.

Test results

The API test details page includes details about the test configuration, uptime associated with the tested endpoint, historical graphs for response time and network timings, as well as the list of individual test results and events.

To troubleshoot a failed test, scroll to the Test Results section and click on one of the failing test results. Review the failed assertions and response details such as returned status code, response time, and associated headers and body to diagnose the issue.

Datadog also has an APM integration with Synthetic Monitoring which allows you to go from a test run that failed to the root cause of the issue by looking at the trace generated by that test run.

Further Reading