Engineering

Never Double Charge: Idempotency Keys in the Coasty Computer Use API

Sarah Chen||4 min
+Enter

You spin up a computer use agent to automate tasks. The API charges $0.05 per agent step. If you retry a POST /v1/runs request because of a network hiccup, you can accidentally pay twice. Idempotency-Key fixes this by making duplicate writes idempotent.

How it works

The POST /v1/runs endpoint accepts an Idempotency-Key header. When the server receives the same key for a new request, it returns the existing run instead of creating a new one. This prevents duplicate runs and charges. The key is an opaque string you choose. The server stores it with the first successful request. Later requests with the same key hit the cached result.

bash
#!/usr/bin/env bash
# POST a run with idempotency to avoid double charges
# Requires COASTY_API_KEY environment variable

API_KEY=$(cat "$HOME/.coasty_key")
BASE_URL="https://coasty.ai/v1"

run_id=$(curl -s -X POST "$BASE_URL/runs" \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: run-$(date +%s)-$(uuidgen)" \
  -d '{
    "machine_id": "my-cloud-vm",
    "task": "open chrome and navigate to https://example.com",
    "cua_version": "v3",
    "max_steps": 20
  }' | jq -r '.id')

echo "Run ID: $run_id"

When you need idempotency

  • Retry POST /v1/runs after a 5xx server error.
  • Retry POST /v1/workflows/{id}/runs after a transient network glitch.
  • Retry POST /v1/runs/{id}/resume after a temporary outage.
  • Use a fresh Idempotency-Key for each logical operation to avoid conflicts.

Always include Idempotency-Key on write operations that charge credits.

Where this beats brittle automation

Traditional automation relies on brittle selectors and API-only tools. If a UI change happens, your script breaks and retries may not help. The Coasty Computer Use API drives real desktops and browsers. The agent interprets the screen and acts like a human. Idempotency keeps your automation reliable even when you retry after errors. You pay only for new work, not for failed retries.

Add Idempotency-Key to your POST /v1/runs requests to prevent duplicate charges. Use it on writes like POST /v1/runs, POST /v1/workflows, and POST /v1/workflows/{id}/runs. Build reliable computer use agents that respect your budget. Get your API key at https://coasty.ai/developers.

Want to see this in action?

View Case Studies
Try Coasty Free