API Key Pattern Regex Pattern
Matches assignments such as api_key=, apiKey:, or token = followed by a value of 20 or more letters, digits, underscores, or hyphens, with optional quotes, case-insensitively; the captured group is the candidate secret. It is a generic catch-all for secret scanning and code review, not a vendor-specific detector: it does not know prefixes like sk_live_ or AKIA and will also flag long non-secret identifiers assigned to a key-like name.
Reviewed May 25, 2026. Privacy model: tool input is processed in your browser and is not uploaded to BytePane servers.
Live Regex Tester
Pattern Breakdown
Code Examples
JavaScript
const regex = /(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?/gi;
const test = "api_key="sk_live_abc123def456ghi789"";
console.log(regex.test(test)); // true
// Extract matches
const matches = test.match(regex);
console.log(matches);Python
import re
pattern = r'(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?'
test = "api_key="sk_live_abc123def456ghi789""
match = re.findall(pattern, test, re.IGNORECASE)
print(match) # Found!Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?`)
fmt.Println(re.MatchString("api_key="sk_live_abc123def456ghi789"")) // true
}Common Use Cases
Match Examples
| Input | Result |
|---|---|
| api_key="sk_live_abc123def456ghi789" | Match |
| name=John | No Match |
About the API Key Pattern Regex
Matches assignments such as api_key=, apiKey:, or token = followed by a value of 20 or more letters, digits, underscores, or hyphens, with optional quotes, case-insensitively; the captured group is the candidate secret. It is a generic catch-all for secret scanning and code review, not a vendor-specific detector: it does not know prefixes like sk_live_ or AKIA and will also flag long non-secret identifiers assigned to a key-like name.
Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The api key pattern pattern is classified as advanced difficulty in the security & auth category. It works in all major programming languages.
When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.
More Security & Auth Patterns
Related Regex Guides
Need More Regex Patterns?
Browse our complete library of 100+ regex patterns with interactive testers.
Frequently Asked Questions
What is the API Key Pattern regex pattern?
Matches assignments such as api_key=, apiKey:, or token = followed by a value of 20 or more letters, digits, underscores, or hyphens, with optional quotes, case-insensitively; the captured group is the candidate secret. It is a generic catch-all for secret scanning and code review, not a vendor-specific detector: it does not know prefixes like sk_live_ or AKIA and will also flag long non-secret identifiers assigned to a key-like name.
How do I use the API Key Pattern regex?
Use the pattern /(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?/gi in your code. In JavaScript: new RegExp('(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["\']?([a-zA-Z0-9_-]{20,})["\']?', 'gi'). Test it above with your own input.
What does this API Key Pattern regex match?
This pattern matches: "api_key="sk_live_abc123def456ghi789"". It does NOT match: "name=John". Secret scanning, code review, security auditing.
Is the API Key Pattern regex beginner-friendly?
This pattern is rated Advanced. It uses advanced features like lookaheads, backreferences, or complex alternation.
What languages support the API Key Pattern regex?
This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.
Can I modify the API Key Pattern regex for my use case?
Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.
Related Tools
JWT Token Regex
Intermediate Security & Auth pattern
UUID (v4) Regex
Intermediate Security & Auth pattern
AWS Access Key Regex
Intermediate Security & Auth pattern
Base64 Encoded String Regex
Beginner Security & Auth pattern
MD5 Hash Regex
Beginner Security & Auth pattern
SHA-256 Hash Regex
Beginner Security & Auth pattern