You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: constructs/api-check.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -423,12 +423,12 @@ You must provide either `entrypoint` or `content`, but not both.
423
423
To define `assertions` for the `request` of an `ApiCheck` you should use the `AssertionBuilder`. The following sources are available for API check assertions:
424
424
425
425
-`statusCode()`: Assert the HTTP status code for the HTTP request, e.g. 200 or 404
426
-
-`jsonBody(property?)`: Assert the JSON response body. The property argument accepts a [JSON path expression](/docs/api-checks/assertions/#json-responses-with-json-path)
426
+
-`jsonBody(property?)`: Assert the JSON response body. The property argument accepts a [JSON path expression](/detect/assertions/#json-responses-with-json-path)
427
427
-`textBody()`: Assert the body as raw text
428
428
-`headers(propery?, regex?)`: Assert a set of response headers, takes the header name as the property argument and a regex to tease out a string from the header value
429
429
-`responseTime()`: Assert the total response time of the HTTP request
430
430
431
-
Learn more in our docs on [Assertions](/docs/api-checks/assertions/).
431
+
Learn more in our docs on [Assertions](/detect/assertions).
@@ -180,7 +171,7 @@ To define `assertions` for the `request` of an `DnsMonitor` you should use the `
180
171
-`responseTime()`: Assert the total response time of the DNS request in milliseconds. Use this to set thresholds for failed lookups
181
172
-`responseCode()`: By default, DNS monitors pass when the return code is NOERROR and fail on error codes (FORMERR, SERVFAIL, NXDOMAIN, etc.). You can override this behavior by defining a custom return code assertion
182
173
-`textAnswer()`: The raw DNS response as plain text. Use this to check for specific strings in the response
183
-
-`jsonAnswer(property?)`: The DNS response in JSON format. This allows you to target specific fields using JSON path assertions. The response structure varies by record type. [Learn more about using JSON path](/docs/api-checks/assertions/#json-responses-with-json-path).
174
+
-`jsonAnswer(property?)`: The DNS response in JSON format. This allows you to target specific fields using JSON path assertions. The response structure varies by record type. [Learn more about using JSON path](/detect/assertions/#json-responses-with-json-path).
Copy file name to clipboardExpand all lines: detect/assertions.mdx
+48-68Lines changed: 48 additions & 68 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,57 +1,46 @@
1
1
---
2
-
title: 'Writing assertions for API checks'
3
-
description: 'Writing assertions for API checks'
2
+
title: 'Defining Assertions'
3
+
description: 'Add assertions to validate your check results'
4
4
sidebarTitle: 'Assertions'
5
5
---
6
6
7
-
8
-
9
-
The response of an API request can be checked for correctness and timeliness by using assertions on the response data. Assertions are flexible statements that combine preset modifiers with custom values to meet the needs of a broad set of use cases.
7
+
Uptime monitors and API checks allow you to define assertions to validate a response and check its data for correctness.
10
8
11
9
## How assertions work
12
10
13
-
Assertions are statements you create that check one aspect of the HTTP response. You can create multiple assertions for one check that assert various aspects of a response, for example:
11
+
Assertions let you validate specific parts of a check's response. For example:
14
12
15
-
- HTTP response status equals 200.
16
-
- HTTP response body equals the text "success".
17
-
- HTTP response time is lower than 2000 milliseconds.
In each assertion, a **source** is connected to a **comparison** and a **target**.
22
21
23
22

24
23
25
-
In some cases a **property** is added, for example when asserting headers or JSON response bodies.
24
+
In some cases a [property](#property) is added, for example when asserting API check headers or JSON response bodies.
26
25
27
26

28
27
29
28
Assertions are executed from top to bottom. If one assertion fails, the full check is considered as failed.
30
29
31
-
## Source
32
-
33
-
On each API check assertion, the following sources are available:
30
+
Supported assertion sources vary by monitor type. Refer to the documentation of the specific monitor (API, DNS, TCP, etc.) to see which sources are available.
34
31
35
-
-**Status code:** The HTTP response status, parsed as an integer.
36
-
-**JSON body:** The response body parsed as a JSON object.
37
-
-**Text body:** The response body as plain text.
38
-
-**Headers:** The response headers as an array of key/value pairs.
39
-
-**Response time:** The response time of the full API request in milliseconds, parsed as an integer value.
32
+
### Property
40
33
41
-
## Property
34
+
The property field is a free-form text input that lets you point to a specific part of the data you want to validate. It’s available for the following types of assertion [sources](#sources):
42
35
43
-
The property field is a free form text field used to identify the name of a header, the part of a JSON object using JSON path or part of a text body.
36
+
-**JSON response bodies**: Use a JSON path expression in the form of dot-separated strings to
37
+
target nested properties in an object, i.e. `$.product.size` or an item in an array, i.e. `$.[1].key`. [Learn more](#json-responses-with-json-path).
44
38
45
-
-With **JSON response bodies**, the property field accepts **JSON path** expressions in the form of dot-separated strings to
46
-
target nested properties in an object, i.e. `$.product.size` or an item in an array, i.e. `$.[1].key`
39
+
-**Text response bodies**: Provide a regular expression with a capture group to pick out parts,
40
+
i.e. `<!doctype (.{4})` would grab the word `html` from a body return `<doctype html>`. [Learn more](#text-body-assertions-with-regular-expressions).
47
41
48
-
- With **text response bodies**, the property field accepts a **regular expression** with a capture group to pick out parts,
49
-
i.e. `<!doctype (.{4})` would grab the word `html` from a body return `<doctype html>`
50
-
51
-
- With **headers**, you provide the header you want to assert in the property field, i.e. `Content-Type`. You can even add a
52
-
regular expression after that to tease out a specific part of the header.
53
-
54
-
Read more about asserting JSON response bodies below.
42
+
-**API check headers**: Enter the header name you want to assert on i.e. `Content-Type`. You can even add a
43
+
regular expression after that to tease out a specific part of the header. [Learn more](#using-regular-expressions).
55
44
56
45
## Comparison
57
46
@@ -75,19 +64,14 @@ Response time is empty? JSON Object is less than? We block out the comparisons w
75
64
76
65
The target field is a free form text field that determines the desired outcome of your assertion.
77
66
78
-
79
67
## JSON responses with JSON path
80
68
81
-
You can use **JSON path** to specify which field of a JSON response body should be asserted. JSON path is a query language
69
+
For monitors that support JSON body assertions, you can use **JSON path** to specify which field of a JSON response body should be asserted. JSON path is a query language
82
70
similar to Xpath for XML, but in general a lot more intuitive and simpler to use.
83
71
84
-
> For Checkly to be able to parse the JSON body, the `Content-Type` header of the response should be set to `application/json`.
85
-
86
72
### JSON path primer
87
73
88
-
JSON path only uses a handful of operators in its queries. Not all of them are useful in the context of assertions.
89
-
Here is an adapted set of examples based on [Stefan Goessner's 2007 introduction post](http://goessner.net/articles/JsonPath/).
90
-
74
+
The following JSONPath operators are available:
91
75
92
76
JSONPath | Description
93
77
-----------------|------------
@@ -153,7 +137,6 @@ JSONPath | Description
153
137
`$.store..price` | The price of everything in the store
154
138
`$.store.book.length` | The length of the book array
155
139
`$..book[2]` | The third book
156
-
`$..book[(@.length-1)]` | The last book via script subscript
157
140
`$..book[-1:]` | The last book via slice
158
141
`$..book[0,1]` | The first two books via subscript union
159
142
`$..book[:2]` | The first two books via subscript array slice.
@@ -163,8 +146,9 @@ JSONPath | Description
163
146
`$..book[?(@.price<30 && @.category=="fiction")]` | Filter all fiction books cheaper than 30
164
147
`$..*` | All members of JSON structure
165
148
166
-
Use this [online editor to play around](https://jsonpath.com/), or look at the examples below. We use this [jsonpath NPM
167
-
module](https://github.com/dchester/jsonpath) under the hood.
149
+
Use this [online editor](https://jsonpath.com/) to try out your own JSONPath expressions.
150
+
151
+
For a full description of the syntax and semantics, see [RFC 9535](https://datatracker.ietf.org/doc/rfc9535/). In addition to the RFC-defined syntax, we support a few convenience extensions (for example, `.length`) to make common assertions easier to write.
168
152
169
153
### Asserting basic types
170
154
@@ -218,23 +202,26 @@ Regular expressions give you the power to extract specific parts of text from a
218
202
You can use regular expressions with two assertions sources:
219
203
220
204
1.**Text body:** Use the property field to add your regex.
221
-
2.**Headers:** First select the header you are interested in the property field, then click "add regex".
205
+
2.**API check headers:** First select the header you are interested in the property field, then click "add regex".
222
206
223
-
Under the hood, we use the stock Javascript regular expressions implementation. Specifically, we use the [.match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
224
-
method. We *do not use the `/g` modifier* and return the first matched group the expression finds.
207
+
We *do not use the `/g` modifier* and return the first matched group the expression finds.
225
208
226
-
Sounds more complicated than it is. Here is an example:
209
+
Here is an example input:
210
+
211
+
```
212
+
The quick brown fox jumps over the lazy dog. It barked.
213
+
```
214
+
215
+
And the following regular expression:
227
216
228
-
```javascript
229
-
constparagraph='The quick brown fox jumps over the lazy dog. It barked.'
230
-
constregex=/quick (.*) fox/
231
-
constfound=paragraph.match(regex)
232
-
console.log(found)
233
-
234
-
// [ 'quick brown fox',
235
-
// 'brown',
236
-
// index: 4,
237
-
// input: 'The quick brown fox jumps over the lazy dog. It barked.' ]
217
+
```
218
+
/quick (.*) fox/
219
+
````
220
+
221
+
The assertion extracts:
222
+
223
+
```
224
+
brown
238
225
```
239
226
240
227
In the example above we return the string `brown` because it is the first capture group, the `(.*)` bit.
@@ -244,25 +231,18 @@ The first item `quick brown fox` is the full match, which we do not return.
244
231
245
232
### Text body assertions with regular expressions
246
233
247
-
If our API returns HTML, there might be a `lang="en"` attribute in the `<html>` element. We can capture the two character
248
-
country/language code value of that attribute with the following expression.
234
+
When a check returns a text-based response, you can use regular expressions to extract and assert on specific parts of the response body.
235
+
236
+
For example, an HTML document might include a `lang="en"` attribute on the `<html>` element. You can capture the two-character language code using the following regular expression:
249
237
250
238

251
239
252
240
The expression `lang="(.{2})"` means 'grab any of the first two characters between `lang="` and the next `"`'. If we were
253
241
sure there are only non-capital characters, we could tighten it up a bit with `lang="([a-z]*)"`.
254
242
255
-
### Header assertions with regular expressions
243
+
### API check header assertions with regular expressions
256
244
257
-
We can use regular expressions with headers too. In this example, we check if the `max-age` property of a `Strict-Transport-Security`
258
-
response header is above a `100000`
245
+
We can use regular expressions with [API check headers](/detect/synthetic-monitoring/api-checks/configuration#headers) too. In this example, we check if the `max-age` property of a `Strict-Transport-Security`
246
+
response header is above a `100000`.
259
247
260
248

A full HTTP request is created by filling out the various parts of the HTTP request settings screen. Not all sections and fields
10
-
are mandatory, only a name and URL are required to get going.
9
+
While configuring up your API check, you can run the request and its assertions using the **Run request** button. This will run the API check on production infrastructure and help you debug any issues.
11
10
12
-
While building up your API check, you can run the request and its assertions using the **Run request** button. This will run the
13
-
API checks on the production infrastructure and help you debug any issues.
11
+
## Basic Setup
14
12
15
-
## Method & URL
13
+
Start by specifying the endpoint you want to track.
16
14
17
-
An API check starts with creating an HTTP request including a HTTP verb (GET, POST, PUT, etc.) and a URL at the minimum.
- Available methods are `GET, POST, PUT, HEAD, DELETE, PATCH`
22
-
- URL's are checked for correctness and must start with either `http://` or `https://`
23
-
24
-
Checking the **"This request should fail"** checkbox allows you to treat HTTP error codes (4xx and 5xx) as correct responses. This comes in handy when testing 404 pages or 500 error handling.
25
-
26
-
When enabled:
27
-
* ✅ Failed responses (4xx, 5xx) → check passes
28
-
* ✅ Successful responses → check passes
29
-
* ❌ Failed assertions → check fails
17
+
-**URL**: The HTTP(S) URL to monitor (e.g. https://checklyhq.com). URL's are checked for correctness and must start with either `http://` or `https://`.
18
+
-**Request methods**: Available methods are `GET, POST, PUT, HEAD, DELETE, PATCH`.
-**This request should fail**: Treat HTTP error codes (4xx and 5xx) as passed. Please note that successful responses still pass. Only failed assertions will cause the check to fail.
30
23
31
24
## Body
32
25
@@ -48,7 +41,6 @@ JSON bodies are commonly used with REST APIs.
48
41
49
42
### GraphQL
50
43
51
-
52
44
This option also sets the `Content-Type` request header to `application/json`, but allows you to type GraphQL queries and
53
45
format them as such, i.e.
54
46
@@ -93,10 +85,8 @@ This section allows you to add query parameters to the request URL in a structur
93
85
94
86
## Basic authentication
95
87
96
-
To add [HTTP basic authentication parameters](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication) to your API
97
-
request, use the username and password fields in the relevant section.
Add [HTTP basic authentication parameters](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication) to your API
89
+
request by entering a username and password in the Authentication tab.
100
90
101
91
## Accessing environment variables
102
92
@@ -167,6 +157,28 @@ parses your spec and prompts you to make some decisions about which requests are
167
157
-**Add a "group" tag:** Copy the name of you spec to a tag and add it to each imported request. This helps
168
158
filtering and grouping related checks in the Checkly dashboard.
169
159
160
+
## Assertions
161
+
162
+
The response of an API request can be checked for correctness and timeliness by using assertions on the response data. This allows you to verify things such as:
163
+
164
+
- HTTP response status equals 200.
165
+
- HTTP response body equals the text "success".
166
+
- HTTP response time is lower than 2000 milliseconds.
On each API check assertion, the following sources are available:
173
+
174
+
-**Status code:** The HTTP response status, parsed as an integer.
175
+
-**JSON body:** The response body parsed as a JSON object. To enable parsing, the Content-Type header of the response should be set to application/json.
176
+
-**Text body:** The response body as plain text.
177
+
-**Headers:** The response headers as an array of key/value pairs.
178
+
-**Response time:** The response time of the full API request in milliseconds, parsed as an integer value.
179
+
180
+
For more details, see our documentation on [Assertions](/detect/assertions).
181
+
170
182
## Responses
171
183
172
184
In almost all cases, you have full access to the HTTP response for assertions. We also store the full response
Copy file name to clipboardExpand all lines: detect/synthetic-monitoring/api-checks/overview.mdx
+3-14Lines changed: 3 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,25 +59,14 @@ API checks perform comprehensive endpoint validation:
59
59
API checks are ideal for testing both internal services and external API dependencies your application relies on.
60
60
</Tip>
61
61
62
-
## Timeouts
62
+
## Response Metrics
63
63
64
-
All API checks are capped at a timeout of **30 seconds**. With each request, we record the most relevant timing phases. This can help you troubleshoot slow responses, e.g. your DNS might be slow.
64
+
With each request, we record the most relevant timing phases:
65
65
66
-
The timing phases correspond to the Node.js request library timing phases:
0 commit comments