Templates/SaaS & accounts

API platform database schema

Developer apps with API keys, webhooks and usage records.

6 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

developers

  • iduuid not null
  • emailtext not null
  • companytext
  • created_attimestamptz not null

apps

  • iduuid not null
  • developer_id> developersuuid not null
  • nametext not null
  • callback_urltext
  • created_attimestamptz not null

api_keys

  • iduuid not null
  • app_id> appsuuid not null
  • prefixtext not null
  • key_hashtext not null
  • last_used_attimestamptz
  • revoked_attimestamptz
  • created_attimestamptz not null

webhook_endpoints

  • iduuid not null
  • app_id> appsuuid not null
  • urltext not null
  • secrettext not null
  • eventstext[] not null
  • created_attimestamptz not null

webhook_deliveries

  • iduuid not null
  • endpoint_id> webhook_endpointsuuid not null
  • eventtext not null
  • status_codeinteger
  • attemptinteger not null
  • delivered_attimestamptz
  • created_attimestamptz not null

usage_records

  • iduuid not null
  • api_key_id> api_keysuuid not null
  • endpointtext not null
  • request_countinteger not null
  • daydate not null
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "api_keys" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "app_id" UUID NOT NULL,
  "prefix" TEXT NOT NULL,
  "key_hash" TEXT NOT NULL UNIQUE,
  "last_used_at" TIMESTAMPTZ,
  "revoked_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "apps" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "developer_id" UUID NOT NULL,
  "name" TEXT NOT NULL,
  "callback_url" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "developers" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL UNIQUE,
  "company" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "usage_records" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "api_key_id" UUID NOT NULL,
  "endpoint" TEXT NOT NULL,
  "request_count" INTEGER NOT NULL,
  "day" DATE NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "webhook_deliveries" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "endpoint_id" UUID NOT NULL,
  "event" TEXT NOT NULL,
  "status_code" INTEGER,
  "attempt" INTEGER DEFAULT 1 NOT NULL,
  "delivered_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "webhook_endpoints" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "app_id" UUID NOT NULL,
  "url" TEXT NOT NULL,
  "secret" TEXT NOT NULL,
  "events" TEXT[] NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "apps" ADD CONSTRAINT "fk_apps_developer_id" FOREIGN KEY ("developer_id") REFERENCES "developers" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "api_keys" ADD CONSTRAINT "fk_api_keys_app_id" FOREIGN KEY ("app_id") REFERENCES "apps" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "webhook_endpoints" ADD CONSTRAINT "fk_webhook_endpoints_app_id" FOREIGN KEY ("app_id") REFERENCES "apps" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "webhook_deliveries" ADD CONSTRAINT "fk_webhook_deliveries_endpoint_id" FOREIGN KEY ("endpoint_id") REFERENCES "webhook_endpoints" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "usage_records" ADD CONSTRAINT "fk_usage_records_api_key_id" FOREIGN KEY ("api_key_id") REFERENCES "api_keys" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

More SaaS & accounts templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.