How to Use Regex to Monitor API Response Bodies
HTTP status codes only tell half the story. A 200 OK means the request completed — not that the response is correct. Here's how regex pattern matching closes that gap.
What's Wrong with Status-Code-Only API Monitoring?
Most API monitoring tools check exactly one thing: did the server respond with a 200? If yes, the monitor turns green and everyone moves on. But HTTP status codes are a terrible proxy for API health.
Consider this scenario:
// HTTP 200 OK — your monitor says "UP" ✓
{
"status": "ok",
"data": null,
"error": "DB_CONN_FAILED"
}Your database connection failed. Your API is returning broken data wrapped in a polite 200 OK. Standard monitors miss this entirely. Your users find out first.
This is called a silent failure — and regex monitoring is the most effective way to catch them.
What is regex body validation?
Instead of stopping at the status code, regex validation runs a regular expression pattern against the actual response body text. If the pattern doesn't match (or does match, depending on your rule), the check is marked as degraded and your alert fires.
In CheckAPI, enable the Use Regex toggle when creating or editing a monitor. Then write your pattern.
What Are Practical Regex Patterns for API Response Validation?
Here are real-world patterns you can use today:
1. Status field must be "ok" or "healthy"
"status":s*"(ok|healthy)"
Fails if the status field contains any other value — including "error", "degraded", or null.
2. Error field must be null
"error":s*null
Catches APIs that return error strings in the error field while still returning 200.
3. Data array must not be empty
"data":s*[[^]]+]
Fails if data is an empty array. Useful for APIs that should always return at least one result.
4. Balance is a positive number
"balance":s*[1-9][0-9]*
For payment or wallet APIs — validates the balance field is a positive integer.
5. No database error string anywhere in body
^(?!.*DB_CONN_FAILED).*$
Negative lookahead — the entire response body must not contain the string DB_CONN_FAILED.
6. Version field exists and is not empty
"version":s*"[^"]+"
Verifies the version field is a non-empty string — useful for health check endpoints.
How Do You Test Regex Patterns Before Going Live?
CheckAPI has a built-in Test Regex button. Paste a sample response body and your pattern — it shows you immediately whether the pattern matches before the monitor goes live. No more regex typos silently making your monitor worthless.
When to use regex vs JSON Path assertions
Regex is flexible and fast to write. JSON Path assertions are more precise for deeply nested JSON fields. A good rule of thumb:
- Use regex for simple string patterns, error detection, and format validation
- Use JSON Path for specific field values, numeric comparisons, and null checks on nested fields
- Combine both for maximum coverage
Try it in CheckAPI
Regex validation is available on all plans including Free. Set up your first regex monitor in under 2 minutes.
Start for Free