DD
DevDash

Regex for Stripe API Key

Regex Pattern

^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$

Stripe publishable, secret, or restricted key format

Quick Answer

The regex pattern for stripe api key is `^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$`. Stripe publishable, secret, or restricted key format. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
pk_test_1234567890abcdefghijklmn✓ Matches
sk_live_ABCDEFGHIJKLMNOPqrstuvwx✓ Matches
pk_1234✗ No match
sk_test_short✗ No match
invalid_key✗ No match

Code Examples

javascript

const regex = /^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$/', $value)) {
    echo "valid";
}

java

String pattern = "^(?:pk|sk|rk)_(?:test|live)_[a-zA-Z0-9]{24,}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.