Azure Functions is Microsoft’s serverless compute platform. Like AWS Lambda and Google Cloud Run, it is stateless — UUID generation happens at the application layer. The Node.js and Python runtimes ship the same crypto APIs as their standalone counterparts. See UUID in AWS Lambda and UUID in GCP Cloud Run for the equivalent patterns on other clouds.
Node.js Azure Function with UUID v7
// src/functions/createOrder.ts
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
import { v7 as uuidv7 } from 'uuid';
import { CosmosClient } from '@azure/cosmos';
const cosmos = new CosmosClient(process.env.COSMOS_CONNECTION_STRING!);
const container = cosmos.database('app').container('orders');
export async function createOrder(
req: HttpRequest,
context: InvocationContext,
): Promise<HttpResponseInit> {
const body = await req.json() as { status: string };
const id = uuidv7();
await container.items.create({
id, // Cosmos DB document ID
status: body.status,
createdAt: Date.now(),
});
return { status: 201, jsonBody: { id } };
}
app.http('createOrder', {
methods: ['POST'],
route: 'orders',
handler: createOrder,
});
UUID v7 as the Cosmos DB id gives the document time-sortable ordering — SELECT * FROM c ORDER BY c.id returns documents chronologically. See UUID v4 vs v7 for the sort order detail.
Python Azure Function with uuid.uuid7()
# function_app.py
import azure.functions as func
import uuid
import json
app = func.FunctionApp()
@app.route(route="orders", methods=["POST"])
def create_order(req: func.HttpRequest) -> func.HttpResponse:
body = req.get_json()
order_id = str(uuid.uuid7()) # Python 3.13+ stdlib
# persist to Azure SQL or Cosmos DB...
return func.HttpResponse(
json.dumps({"id": order_id}),
status_code=201,
mimetype="application/json",
)
Python 3.13’s uuid.uuid7() is available on the Azure Functions Python 3.13 runtime. For earlier runtimes, use the uuid6 backport. See Python 3.13 uuid.uuid7().
Azure SQL — UUID v7 primary key
import sql from 'mssql';
import { v7 as uuidv7 } from 'uuid';
const pool = await sql.connect(process.env.SQL_CONNECTION_STRING!);
async function createOrder(status: string): Promise<string> {
const id = uuidv7();
await pool.request()
.input('id', sql.UniqueIdentifier, id)
.input('status', sql.VarChar(32), status)
.query('INSERT INTO orders (id, status) VALUES (@id, @status)');
return id;
}
Azure SQL’s uniqueidentifier column stores 128-bit UUIDs — UUID v7 is stored identically to v4. ORDER BY id on a UUID v7 primary key returns rows in creation-time order. See UUID in SQL Server for the full Azure SQL / SQL Server UUID guide.
Idempotent function with UUID v5
For Azure Functions triggered by Service Bus or Event Hub, use UUID v5 as a deterministic idempotency key:
import { v5 as uuidv5 } from 'uuid';
const NAMESPACE = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; // URL namespace
export async function processEvent(
message: ServiceBusMessage,
context: InvocationContext,
): Promise<void> {
// Same message body always produces the same key
const idempotencyKey = uuidv5(JSON.stringify(message.body), NAMESPACE);
const exists = await db.query(
'SELECT 1 FROM processed_events WHERE event_id = $1',
[idempotencyKey]
);
if (exists.rowCount > 0) return;
await processAndMark(message, idempotencyKey);
}
See UUID as Idempotency Keys and UUID Namespace for the v5 deterministic pattern.
Further reading
- UUID in AWS Lambda
- UUID in GCP Cloud Run
- UUID in Serverless — full guide
- UUID in SQL Server
- UUID as Idempotency Keys
- UUID v7 for Cursor Pagination
External references
Frequently asked questions
Does crypto.randomUUID() work in Azure Functions?
Yes. Azure Functions Node.js runtimes from Node 18 onwards include crypto.randomUUID() natively. Python 3.13 runtimes include uuid.uuid7() in the standard library. No extra package is needed for UUID v4 in Node.js. For UUID v7 in Node.js, install the uuid npm package. See crypto.randomUUID() runtime support.
Should I use UUID v7 or Cosmos DB's auto-generated IDs?
Cosmos DB's auto-generated IDs are random strings — similar to UUID v4. UUID v7 is preferable for Cosmos DB documents because its timestamp prefix enables time-range queries on the document ID field without a secondary index, and it integrates cleanly with SQL databases (Azure SQL, PostgreSQL) that use the same IDs. UUID v7 also sorts lexicographically in creation order, simplifying cursor-based pagination. See UUID v7 for Cursor Pagination.