Dan MercerD
Convex Community3y ago
4 replies
Dan Mercer

How to generate random base64 in mutation

I have a "create API key" mutation and I'd like to generate a base64 API key, but it seems the btoa provided by the Convex function runtime fails if the string contains values outside a certain range.

This newApiKey() function works in browsers but fails in Convex: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABGApgdwIIAcYGkUCeAFAJQBciAzlAE4xgDmiA3gFCKID0niDKqNAIZQUiAMwAmALQAjAiMQYACgElEAa0LtEEBNURyRlRAF5FNIQQB0wGnAC2RZogA2-BlAAWFSYgC+ADSIpKYAfIgAssKeNi5wcDREUV5WQmAAJg4hAFSIEgCsAGwkJADc2tyI-LrpovSIAKoASgAyUpSCwKIygpQohQAsFTxEfaKeUFBYlGTc-FZoMOowWCjpMIJWCQyci8ucAEK9-QMAxABqgnSCYFCUAPqUIPb2VwT3UIIybiTaNChQEA0JAyKBwQREADKtHoDBsdnsAGFPFdEXBalZBFgsC5iGAQC4XEFDChKCVtBxUigcYIICgiJwADoAak4DCCAHIpBzfhxKf8aXSGYzuOzEBz7jyKYgqYL6ZwTGzOVK-KxWII3DQoERUJgcPhiCUgA

Is there an alternative way to generate a random base64 string?
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Was this page helpful?