Zum Hauptinhalt springen

API-Referenz

Roestify nutzt eine hybride API-Architektur: tRPC für alle App-Operationen, REST-Endpoints für Webhooks, OAuth und öffentliche APIs.

tRPC (Primäre API)

Endpoint

/api/trpc/{router.procedure}

Setup

  • Server: tRPC v11.0.0 mit SuperJSON-Transformer
  • Client: @trpc/react-query mit @tanstack/react-query
  • Validierung: Zod v4.3.5 via .input()
  • Context: Session (Auth.js), DB (Drizzle), userId, tenantId, role

Procedures

TypAuthBeschreibung
publicProcedureKeineHealth Checks, öffentliche Displays
protectedProcedureSessionApp-Logik, injiziert userId/tenantId/role

Router-Module

RouterZuständigkeit
productionLots, Lieferanten, Maschinen, Profile, Tonnen, Röst-Log, Varianten, Abpackung, Wartung
b2bKunden, Bestellungen, Rechnungen, Abos, Preise, Forecasts
analyticsBerichte, Metriken
tenantEinstellungen, Team, Onboarding, Billing
online-storeShopify-Integration
shippingDHL-Versand
billingStripe, Subscriptions
traceabilityRückverfolgung

Beispiel

// Server (src/server/trpc/routers/production.ts)
export const productionRouter = createTRPCRouter({
lots: {
list: protectedProcedure
.input(z.object({ status: z.enum(['Active', 'Depleted', 'Blocked']).optional() }))
.query(async ({ ctx, input }) => {
return ctx.db.select().from(greenCoffeeLots)
.where(eq(greenCoffeeLots.tenantId, ctx.session.user.tenantId));
}),
},
});

// Client (React Component)
const { data } = trpc.production.lots.list.useQuery({ status: 'Active' });

REST-Endpoints

REST-Routes unter /api/v1/ für Fälle, die nicht in tRPC passen:

Authentifizierung

REST-Endpoints nutzen verschiedene Auth-Mechanismen:

MechanismusVerwendung
Session JWTEingeloggte User (Cookies von Auth.js)
Bearer TokenCron-Jobs (CRON_SECRET), Platform-Admin
HMAC / SignaturShopify OAuth, Stripe Webhooks
Token (URL)Display-API, Einladungen, Bestellformular

Response-Format

// Erfolg
{ "data": { ... } }

// Fehler
{ "error": "Beschreibung des Fehlers" }

HTTP-Status-Codes

CodeBedeutung
200Erfolg
201Erstellt
400Validierungsfehler
401Nicht authentifiziert
403Keine Berechtigung / Usage Limit
404Nicht gefunden
409Konflikt (Duplikat, Statusverletzung)
410Abgelaufen (Token)
429Rate Limit überschritten
500Server-Fehler

Rate Limiting

In-Memory Rate Limiter mit Sliding-Window-Ansatz. Bei Überschreitung:

HTTP 429 Too Many Requests

REST-Endpunkte nach Domain

DomainBasis-PfadBeschreibung
Auth/api/auth/Auth.js Endpunkte
Admin/api/v1/admin/Plattform-Administration
Shopify/api/v1/shopify/OAuth + Sync
Reports/api/v1/reports/Finanzberichte
Settings/api/v1/tenant-settings/Logo, Display
Cron/api/v1/cron/Hintergrund-Jobs
Order-Form/api/v1/order-form/B2B-Bestellformular (OTP-Auth)
Billing/api/v1/billing/Stripe Webhooks
Invitations/api/v1/invitations/Einladungs-Annahme