Tips > Building Workflows

Use `$execution.id` for Idempotency Keys

When your workflow calls external APIs that charge money or trigger irreversible actions (sending emails, creating payments, posting to social media), use `$...

TipIntermediate1 min read

When your workflow calls external APIs that charge money or trigger irreversible actions (sending emails, creating payments, posting to social media), use $execution.id combined with an item index to generate idempotency keys. This prevents duplicate actions if n8n retries the workflow or the node executes twice.

Real-world example: Creating Stripe payment intents where a duplicate call would charge the customer twice.

// Mode: Run Once for Each Item
const item = $input.item.json;
const itemIndex = $input.all().indexOf($input.item);

// Deterministic idempotency key: same execution + same item = same key
const idempotencyKey = `${$execution.id}_${itemIndex}_${item.orderId}`;

const response = await this.helpers.httpRequest({
  method: 'POST',
  url: 'https://api.stripe.com/v1/payment_intents',
  headers: {
    'Authorization': `Bearer ${item.stripeSecretKey}`,
    'Idempotency-Key': idempotencyKey,
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: `amount=${item.amount}&currency=${item.currency}&customer=${item.customerId}`,
});

return [{
  json: {
    ...item,
    paymentIntentId: response.id,
    idempotencyKey,
    status: response.status,
  }
}];
```text
> **Warning: Idempotency Key Scope**
>
> The key is unique per execution. If you **manually re-execute** the workflow, `$execution.id` changes and a new payment intent will be created. For truly bulletproof idempotency, derive the key from business data only (e.g., `orderId + invoiceDate`).

**Related:** [Use Edit Fields in "Map Each" Mode for Simple Renames](../data-transformation/01-use-edit-fields-in-map-each-mode-for-simple-renames.md) | [Configure Payload Size and Binary Data Mode for Large Files](../performance-and-large-files/01-configure-payload-size-and-binary-data-mode-for-large-files.md)

Want this running in your stack?

I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.