BytePane

API Key Pattern Regex Pattern

Detects potential API keys and tokens in source code. Used by security tools to prevent accidental secret exposure.

🔐
Security & Auth
Advanced
Difficulty
Universal
Language
gi
Flags
// Regular Expression
/(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?/gi

Live Regex Tester

Pattern Breakdown

(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

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

Secret scanningcode reviewsecurity auditing

Match Examples

InputResult
api_key="sk_live_abc123def456ghi789"Match
name=JohnNo Match

About the API Key Pattern Regex

Detects potential API keys and tokens in source code. Used by security tools to prevent accidental secret exposure.

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.

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?

Detects potential API keys and tokens in source code. Used by security tools to prevent accidental secret exposure.

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