PUT /api/v1/preferences/theme • HTTP 200 OK

Idempotent Upsert API Example (Create or Update) — Request & Response Example

Demonstrates an idempotent upsert endpoint where sending the request multiple times produces the exact same end state without duplicate entries.

1. HTTP Client Request (PUT)

PUT /api/v1/preferences/theme HTTP/1.1
{
  "Content-Type": "application/json"
}

{
  "mode": "dark",
  "high_contrast": true,
  "font_size": 14
}

2. Server JSON Response (HTTP 200 OK)

{
  "status": "saved",
  "updated": true,
  "settings": {
    "mode": "dark",
    "high_contrast": true,
    "font_size": 14
  }
}

Copy-Paste Code Snippets (cURL, JavaScript Fetch, Python, Node.js)

curl -X PUT "https://cometflow.space/api/v1/preferences/theme" \
  -H "Content-Type: application/json" \
  -d '{"mode": "dark", "high_contrast": true}'
fetch('https://cometflow.space/api/v1/preferences/theme', {
  method: 'PUT',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ mode: 'dark', high_contrast: true })
}).then(res => res.json()).then(console.log);
import requests

res = requests.put("https://cometflow.space/api/v1/preferences/theme", json={"mode": "dark"})
print(res.json())

How This API Call Works Step-by-Step

  1. 1. Client sends desired state.
  2. 2. Database executes INSERT ... ON CONFLICT DO UPDATE.
  3. 3. Safe to retry automatically on network failures because it is 100% idempotent.

Backend API Functions: The API function upsertPreference(req, res) uses database upsert logic to ensure zero duplicated records.

Frequently Asked Questions

What does idempotent mean in API design?

An API method is idempotent if the side-effects of making multiple identical requests are the same as making a single request. GET, PUT, and DELETE are idempotent; POST is not.