Skip to content
N1
Integrations · Go · REST API

An online store REST API in Go: orders without duplicates and stock that never goes negative

Client: Personal project Category: Integrations Stack: Go 1.25, chi, PostgreSQL 16, pgx, sqlc, golang-migrate, JWT, bcrypt, zap, Docker Compose, GitHub Actions, golangci-lint Timeline: personal project, 2025
Problem

A store API was needed that correctly handles typical production-traffic situations: a repeated order submission after a dropped connection and simultaneous purchases of the last units of a product.

Constraints

A repeated request must not create a second order, stock must not go negative under concurrent orders, and amounts must be calculated without float rounding errors.

What I did
  • JWT authentication: a 15-minute access token and a refresh token stored in the database only as a hash; user and admin roles, bcrypt passwords
  • Order idempotency via the Idempotency-Key header: a repeated request returns the stored response, and keys live for 24 hours
  • Atomic stock deduction in a transaction: a conditional UPDATE prevents going negative, and order cancellation returns goods to stock
  • A data layer on sqlc with golang-migrate migrations, amounts in decimal rather than float
  • Catalog pagination, filtering and sorting, input validation, per-IP rate limiting, graceful shutdown, structured zap logs
  • OpenAPI 3.0 (10 paths) and a Postman collection; CI: build, migrations, 20 unit tests with -race and golangci-lint
Architecture
chi router + middleware → Service layer → sqlc repositories → PostgreSQL 16 (transactions) → OpenAPI / Postman
Result

The API covers the "registration → catalog → order → payment or cancellation" cycle and behaves predictably under retries and concurrent orders; every change is checked in CI.

What can be reused
POST request idempotency via Idempotency-Key Atomic stock deduction with a conditional UPDATE in a transaction Go service skeleton: chi, sqlc, zap, CI with golangci-lint