#Random uuid
5 messages · Page 1 of 1 (latest)
Hi @austere lynx. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
The knowledge sources you provided don’t mention UUID generation specifically, nor do they show concrete examples of using Node’s crypto/uuid APIs with Astro.
They do confirm that:
- You’re using the Node adapter, which runs your code in a Node.js environment.[@astrojs/node guide]
- Astro supports Node built-ins via the
node:prefix (with some adapter/runtime caveats).[Node builtins]
Given that, the usual Node approach should work in routes/pages that run on the server (e.g. output: 'server' or prerender = false):
// Example: src/pages/random-uuid.js
export const prerender = false; // ensure this runs on the server
export async function GET() {
// In Node 18+, you can use crypto.randomUUID()
const { randomUUID } = await import('node:crypto');
const id = randomUUID();
return new Response(
JSON.stringify({ uuid: id }),
{ headers: { 'Content-Type': 'application/json' } }
);
}
Because the docs don’t explicitly show UUID usage, if you run into issues, check:
- Your Node version (for
crypto.randomUUID()support). - That the route is actually rendered on-demand (server) and not fully prerendered. [On‑demand rendering]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@austere lynx Kapa.ai is still learning and improving, please let me know how I did by reacting below.
You can also use nanoid
https://github.com/ai/nanoid?tab=readme-ov-file#benchmark