Server-side `user_id` signing
When a public (`pk_`) key has **Require signed user_id** enabled, the browser must present a signature proving your backend vouches for the user. This stops an anonymous visitor from minting a session as someone else.
The signature is HMAC-SHA256(secret, "<user_id>|<unix_ts>"), hex-encoded, where
secret is the key's HMAC secret (shown once at key creation — store it as a
server-side env var, never ship it to the client). The timestamp must be within
±5 minutes of the server clock.
Send three fields to the browser, which passes them into the SDK's getUser():
user_id, user_id_sig, user_id_ts.
Node / TypeScript#
Or without the SDK:
Python#
Ruby#
Notes#
- The secret is hex — decode it to raw bytes before HMAC-ing (
Buffer.from(…, 'hex'),bytes.fromhex(…),pack('H*')). - Re-sign per page load (or whenever the user changes); signatures expire after 5 minutes.
- All four implementations above produce identical signatures for the same inputs, so any backend language interoperates with the browser SDK.