APIs and backends

Product photo background removal and resize API

Node.js and Postgres API that removes photo backgrounds, outputs five sizes, with prepaid credits, per-customer API keys, usage dashboard and batch webhooks.

nodejstypescriptpostgrespg-bosssharps3

Built 1 time, downloaded 0 times.

Install from your terminal

Run this in your project folder. It signs you in, shows what changes and asks before it overwrites a file.

npx claude-lab add product-photo-background-removal-and-resize-api-c82759
10 filesDownload zip
CLAUDE.md
# Product photo background API

## Purpose

This API takes a product photo and returns a clean background-removed image plus five resized copies. Customers buy prepaid credits, authenticate with per-customer API keys, check usage on a dashboard, and submit batches that report back through signed webhooks.

## Stack

- Node.js with TypeScript (ESM).
- Postgres for data, plus pg-boss as the job queue (backed by Postgres).
- sharp for resizing and format conversion.
- Background removal sits behind an interface so the provider can be swapped.
- Object storage through an S3-compatible client.

## Commands

Check these against package.json after `npm init` and update any that differ.

- `npm run dev`: start the API locally.
- `npm test`: unit tests.
- `npm run test:integration`: tests against real Postgres (needs docker compose).
- `npm run typecheck`: TypeScript check with no emit.
- `npm run lint`: linter.
- `npm run build`: production build.
- `npm run migrate`: apply migrations to the database in `.env`.
- `docker compose up -d db`: start local Postgres.

## Layout

- `src/api`: routes and request validation.
- `src/jobs`: queue workers.
- `src/billing`: credits ledger and deduction logic.
- `src/auth`: API keys and auth middleware.
- `src/images`: upload checks and the image pipeline.
- `src/webhooks`: signing, delivery and retries.
- `src/dashboard`: usage endpoints and page.
- `migrations/`: forward-only SQL migrations.

## Credits rules

- The credit ledger is append-only. Never update or delete rows.
- Balance is the sum of ledger entries.
- Deduct inside one DB transaction with a unique idempotency key.
- Refund when processing fails.
- Never let the balance go below zero.
- Credits are integers. Never use floats.

## API key rules

- Store only a SHA-256 hash and a short prefix.
- Show the full key once, at creation.
- Never log keys or Authorization headers.
- Compare hashes in constant time.

## Webhook rules

- Sign each request with HMAC-SHA256 in a header.
- Include a timestamp so receivers can reject replays.
- Retry with exponential backoff.
- Record every delivery attempt in `webhook_deliveries`.
- Block private, loopback and link-local addresses, and require https.

## Image rules

- Validate file type by magic bytes, not the file extension.
- Cap file size and pixel count.
- Strip EXIF metadata.
- Run image work in workers, never in request handlers.
- Define the five output sizes in one config constant.

## Gotchas

- Tests use a real Postgres from docker compose. Never mock the ledger.
- The background removal provider is faked in tests.
- No secrets in the repo. Use `.env.example` with placeholders such as `YOUR_API_KEY` and `YOUR_STORAGE_BUCKET`.
- Never commit `.env`.

## Workflow

- Explore, plan, code, then run the verify skill before saying a task is done.
- Use `/clear` between unrelated tasks and `/compact` at natural breaks.
- Use `/usage` to check token spend.

## Review

@REVIEW.md

## More detail

- Full API behavior and data model: SPEC.md.
- Phase-by-phase build plan with /goal prompts: PLAN.md.
Use for my project