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

TokenDescription
(?:Start of non-capturing group
sMatches the literal character 's'
kMatches the literal character 'k'
|Alternation — matches the expression before OR after the pipe
pMatches the literal character 'p'
kMatches the literal character 'k'
|Alternation — matches the expression before OR after the pipe
aMatches the literal character 'a'
pMatches the literal character 'p'
iMatches the literal character 'i'
|Alternation — matches the expression before OR after the pipe
kMatches the literal character 'k'
eMatches the literal character 'e'
yMatches 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
lMatches the literal character 'l'
iMatches the literal character 'i'
vMatches the literal character 'v'
eMatches the literal character 'e'
|Alternation — matches the expression before OR after the pipe
tMatches the literal character 't'
eMatches the literal character 'e'
sMatches the literal character 's'
tMatches the literal character 't'
|Alternation — matches the expression before OR after the pipe
pMatches the literal character 'p'
rMatches the literal character 'r'
oMatches the literal character 'o'
dMatches the literal character 'd'
|Alternation — matches the expression before OR after the pipe
dMatches the literal character 'd'
eMatches the literal character 'e'
vMatches 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

InputExpected
sk_test_4eC39HqLyjWDarjtT1zdp7dcMatch
pk_live_abc123def456ghi789jkl012Match
api_key_1234567890abcdefMatch
random_string_hereNo Match
mypassword123No Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

sk_test_4eC39HqLyjWDarjtT1zdp7dc pk_live_abc123def456ghi789jkl012 api_key_1234567890abcdef random_string_here mypassword123

Matches & Capture Groups

#1sk_test_4eC39HqLyjWDarjtT1zdp7dcindex 0
#2pk_live_abc123def456ghi789jkl012index 33
#3key_1234567890abcdefindex 70
Pattern: 68 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →