Regex to Match API Key Formats
Detect common API key formats with standard prefixes like sk_, pk_, api_, and key_ followed by alphanumeric strings of 16-64 characters. Free regex tester.
Regular Expression
/(?:sk|pk|api|key)[-_]?(?:live|test|prod|dev)?[-_]?[A-Za-z0-9]{16,64}/g
Token Breakdown
| Token | Description |
|---|---|
| (?: | Start of non-capturing group |
| s | Matches the literal character 's' |
| k | Matches the literal character 'k' |
| | | Alternation — matches the expression before OR after the pipe |
| p | Matches the literal character 'p' |
| k | Matches the literal character 'k' |
| | | Alternation — matches the expression before OR after the pipe |
| a | Matches the literal character 'a' |
| p | Matches the literal character 'p' |
| i | Matches the literal character 'i' |
| | | Alternation — matches the expression before OR after the pipe |
| k | Matches the literal character 'k' |
| e | Matches the literal character 'e' |
| y | Matches the literal character 'y' |
| ) | End of group |
| [-_] | Character class — matches any one of: -_ |
| ? | Makes the preceding element optional (zero or one times) |
| (?: | Start of non-capturing group |
| l | Matches the literal character 'l' |
| i | Matches the literal character 'i' |
| v | Matches the literal character 'v' |
| e | Matches the literal character 'e' |
| | | Alternation — matches the expression before OR after the pipe |
| t | Matches the literal character 't' |
| e | Matches the literal character 'e' |
| s | Matches the literal character 's' |
| t | Matches the literal character 't' |
| | | Alternation — matches the expression before OR after the pipe |
| p | Matches the literal character 'p' |
| r | Matches the literal character 'r' |
| o | Matches the literal character 'o' |
| d | Matches the literal character 'd' |
| | | Alternation — matches the expression before OR after the pipe |
| d | Matches the literal character 'd' |
| e | Matches the literal character 'e' |
| v | Matches the literal character 'v' |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| [-_] | Character class — matches any one of: -_ |
| ? | Makes the preceding element optional (zero or one times) |
| [A-Za-z0-9] | Character class — matches any one of: A-Za-z0-9 |
| {16,64} | Matches between 16 and 64 times |
Detailed Explanation
This regex detects common API key formats used by popular services. Here is the token-by-token breakdown:
(?:sk|pk|api|key) — A non-capturing group matching common API key prefixes. sk is used by Stripe for secret keys, pk for publishable keys, api is a generic prefix, and key is another common convention.
[-_]? — Optionally matches a hyphen or underscore separator between the prefix and the optional environment indicator.
(?:live|test|prod|dev)? — An optional non-capturing group matching environment indicators. Many services include the environment in the key: live for production, test for testing, prod for production, and dev for development.
[-_]? — Another optional hyphen or underscore separator between the environment indicator and the key body.
[A-Za-z0-9]{16,64} — Matches the main key body consisting of 16 to 64 alphanumeric characters. This range covers most API key lengths: Stripe keys are typically 32 characters, AWS keys are 20 characters, and other services vary within this range.
The g flag enables global matching to find all API keys in the text. This pattern is designed for security auditing and secret detection. It identifies potentially exposed API keys in source code, configuration files, logs, and documentation.
This pattern catches common formats like sk_test_abc123..., pk_live_xyz789..., api_key_abc..., and key-prod-xyz.... It is used by security scanners, pre-commit hooks, and CI/CD pipelines to prevent accidental credential exposure. Note that this is a heuristic pattern and may produce false positives for non-key strings with similar formats.
Example Test Strings
| Input | Expected |
|---|---|
| sk_test_4eC39HqLyjWDarjtT1zdp7dc | Match |
| pk_live_abc123def456ghi789jkl012 | Match |
| api_key_1234567890abcdef | Match |
| random_string_here | No Match |
| mypassword123 | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
68 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex