X Enterprises
fastify-xconfig

xRandomUUID() / xGenerateUUID()

Generates a cryptographically-random UUID v4 string using uncrypto.

xRandomUUID() / xGenerateUUID()

Generates a UUID v4 string using uncrypto's randomUUID, which works in Node.js, edge runtimes, and browser environments. fastify.xGenerateUUID is a direct reference to the underlying randomUUID function; fastify.xRandomUUID is a thin wrapper that calls it.

Signature

fastify.xRandomUUID(): string
fastify.xGenerateUUID(): string  // alias

Params

Both decorators take no arguments.

Returns

string — a UUID v4 in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx.

Throws

Never throws under normal conditions. Throws if the runtime lacks a secure entropy source (extremely rare; only in stripped-down environments).

Examples

Generate an ID for a new record

fastify.post("/uploads", async (request) => {
  const id = fastify.xRandomUUID();

  const upload = await fastify.prisma.upload.create({
    data: { id, filename: request.body.filename },
  });

  return upload;
});

Idempotency key for an external API call

fastify.post("/payments", async (request) => {
  const idempotencyKey = fastify.xGenerateUUID();

  const result = await stripeClient.paymentIntents.create(
    { amount: request.body.amount, currency: "usd" },
    { idempotencyKey }
  );

  return result;
});

See also

AI Context

package: "@xenterprises/fastify-xconfig"
methods: fastify.xRandomUUID() | fastify.xGenerateUUID() (alias)
use-when: Generate a UUID v4 string
returns: string
Copyright © 2026