PHP

Generate Secure API Key

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Mint an API key with a recognizable prefix ("sk_live_…") and 32 bytes of crypto-random entropy encoded as URL-safe base64. Stripe-style readable IDs.
PHP
Raw
<?php
function generateApiKey(string $prefix = 'sk_live_'): string {
    $random = rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
    return $prefix . $random;
}

echo generateApiKey();             // sk_live_xV9...
echo generateApiKey('pk_test_');   // pk_test_q4F...
echo generateApiKey('user_');      // user_2Lk...
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.