# sukhrajdev/apixy architecture

> Architecture diagram of the GitHub repository sukhrajdev/apixy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sukhrajdev/apixy
- Repository: https://github.com/sukhrajdev/apixy

## Overview

#### Purpose and Entry Points
Apixy is a Node.js/Express REST backend that manages accounts and user-owned LLM provider keys, then proxies Google Gemini requests using those stored credentials. The HTTP process starts in `backend/src/server.ts`; route modules split the public API into auth, token refresh, user, and provider-key/LLM surfaces: `backend/src/routes/auth.route.ts`, `backend/src/routes/api.route.ts`, and `backend/src/routes/user.route.ts`.

#### HTTP Layer and Request Dispatch
Controllers translate HTTP requests into service calls and response envelopes. Auth, user lifecycle, and API-key/LLM operations are separated in `backend/src/controllers/auth.controller.ts`, `backend/src/controllers/user.controller.ts`, and `backend/src/controllers/api.controller.ts`. Routes are the primary boundary between untrusted request data and application logic; user input validation is represented by Zod schema code in `backend/src/utils/zod/schema/user.schema.ts`.

#### Authentication and Token Lifecycle
Authentication supports registration, login/logout, email verification, password change, and access-token refresh. JWT issuance/verification is centralized in `backend/src/providers/token/token.provider.ts`; auth orchestration is in `backend/src/services/auth.service.ts`. Protected routes use bearer-token middleware in `backend/src/middlewares/auth.middleware.ts`, while refresh tokens are supplied through login-set HTTP-only cookies and the token route at `backend/src/routes/token.route.ts`.

#### User and Credential Domain
Prisma persists `user` records, including account credentials and token-related state, and `api` records owned by users for provider metadata, encrypted/stored key material, and generated API tokens. The authoritative relational model and PostgreSQL mappings are `backend/prisma/schema.prisma`, with database evolution under `backend/prisma/migrations/`. User read/update/delete behavior belongs to `backend/src/services/user.service.ts`; provider-key CRUD and LLM invocation orchestration belong to `backend/src/services/api.service.ts`.

#### Provider-Key Protection and LLM Invocation
A user first creates a Google provider-key record through authenticated API routes. Later, an LLM request identifies a provider/model and includes the stored API token (`skKey`); API-token middleware/service logic resolves and authorizes the saved credential before use. This boundary is implemented across `backend/src/middlewares/Apitoken.middleware.ts`, `backend/src/services/api.service.ts`, and `backend/src/providers/llm/google/google.llm.ts`. Only Google Gemini is materially implemented, via the Google GenAI SDK; schema enum support for other providers does not imply runtime integration.

#### External Services and Infrastructure
PostgreSQL is accessed via Prisma configuration/client setup in `backend/src/configs/prisma.config.ts` and `backend/prisma.config.ts`. Gmail SMTP/Nodemailer sends verification messages through `backend/src/providers/email/email.provider.ts` and `backend/src/providers/email/templates/verification.template.ts`. Environment variables provide database connectivity, JWT secrets/expirations, and SMTP credentials. A Redis configuration exists at `backend/src/configs/redis.config.ts`, but the supplied structure does not establish it as a required active request-path dependency.

#### SDK Consumer Layer
A separate TypeScript SDK packages client-facing request and chat abstractions for calling this backend rather than hosting another service. Its public export is `sdk/src/index.ts`; transport and chat flows are centered in `sdk/src/core/request.core.ts` and `sdk/src/core/chat.core.ts`. This creates a clean consumer boundary: SDK callers supply API base/client data, while the backend remains responsible for identity, stored provider credentials, authorization, and Gemini execution.

Main entry points: [`backend/src/routes/api.route.ts`](https://github.com/sukhrajdev/apixy/blob/main/backend/src/routes/api.route.ts) (Public routes), [`backend/src/configs/prisma.config.ts`](https://github.com/sukhrajdev/apixy/blob/main/backend/src/configs/prisma.config.ts) (Prisma client), [`backend/src/services/auth.service.ts`](https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/auth.service.ts) (Auth service), [`backend/src/services/api.service.ts`](https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/api.service.ts) (Provider-key & LLM service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend API"]
  node_server["Express server<br/>Node entry point<br/>[server.ts]"]
  node_routes["Public routes<br/>HTTP routing<br/>[api.route.ts]"]
  node_auth_controller["Auth controller<br/>HTTP controller<br/>[auth.controller.ts]"]
  node_user_controller["User controller<br/>HTTP controller<br/>[user.controller.ts]"]
  node_api_controller["API &amp; LLM controller<br/>HTTP controller<br/>[api.controller.ts]"]
  node_validation["User schemas<br/>Zod validation<br/>[user.schema.ts]"]
  node_auth_middleware["Bearer auth<br/>Express middleware<br/>[auth.middleware.ts]"]
  node_api_token_middleware["Stored API-token auth<br/>Express middleware"]
  node_auth_service["Auth service<br/>Application service<br/>[auth.service.ts]"]
  node_token_provider["JWT provider<br/>Token provider<br/>[token.provider.ts]"]
  node_user_service["User service<br/>Application service<br/>[user.service.ts]"]
  node_api_service["Provider-key &amp; LLM service<br/>Application service<br/>[api.service.ts]"]
  node_google_llm["Google Gemini adapter<br/>LLM provider adapter<br/>[google.llm.ts]"]
  node_email_provider["Verification email<br/>Email provider<br/>[email.provider.ts]"]
end

subgraph group_data["Data &amp; external services"]
  node_prisma_client["Prisma client<br/>Database access<br/>[prisma.config.ts]"]
  node_schema["Relational schema<br/>Prisma schema<br/>[schema.prisma]"]
  node_postgres[("PostgreSQL<br/>Database")]
  node_gmail["Gmail SMTP<br/>External email service"]
end

subgraph group_sdk["TypeScript SDK"]
  node_sdk_index["SDK public API<br/>TypeScript package export<br/>[index.ts]"]
  node_sdk_request["SDK request transport<br/>SDK core<br/>[request.core.ts]"]
  node_sdk_chat["SDK chat API<br/>SDK core<br/>[chat.core.ts]"]
end

node_sdk_index -->|"exports"| node_sdk_request
node_sdk_index -->|"exports"| node_sdk_chat
node_sdk_chat -->|"uses transport"| node_sdk_request
node_sdk_request -->|"HTTP requests"| node_server
node_server -->|"mounts"| node_routes
node_routes -->|"validates input"| node_validation
node_routes -->|"protects user routes"| node_auth_middleware
node_routes -->|"guards LLM calls"| node_api_token_middleware
node_routes -->|"auth endpoints"| node_auth_controller
node_routes -->|"user endpoints"| node_user_controller
node_routes -->|"key and LLM endpoints"| node_api_controller
node_auth_controller -->|"auth operations"| node_auth_service
node_auth_service -->|"issues tokens"| node_token_provider
node_auth_middleware -->|"verifies bearer JWT"| node_token_provider
node_auth_service -->|"sends verification"| node_email_provider
node_email_provider -->|"SMTP"| node_gmail
node_user_controller -->|"user lifecycle"| node_user_service
node_api_controller -->|"credential and invocation"| node_api_service
node_api_token_middleware -->|"authorizes skKey"| node_api_service
node_api_service -->|"Gemini request"| node_google_llm
node_auth_service -->|"account state"| node_prisma_client
node_user_service -->|"user records"| node_prisma_client
node_api_service -->|"provider credentials"| node_prisma_client
node_prisma_client -->|"maps model"| node_schema
node_prisma_client -->|"queries"| node_postgres

click node_server "https://github.com/sukhrajdev/apixy/blob/main/backend/src/server.ts"
click node_routes "https://github.com/sukhrajdev/apixy/blob/main/backend/src/routes/api.route.ts"
click node_auth_controller "https://github.com/sukhrajdev/apixy/blob/main/backend/src/controllers/auth.controller.ts"
click node_user_controller "https://github.com/sukhrajdev/apixy/blob/main/backend/src/controllers/user.controller.ts"
click node_api_controller "https://github.com/sukhrajdev/apixy/blob/main/backend/src/controllers/api.controller.ts"
click node_validation "https://github.com/sukhrajdev/apixy/blob/main/backend/src/utils/zod/schema/user.schema.ts"
click node_auth_middleware "https://github.com/sukhrajdev/apixy/blob/main/backend/src/middlewares/auth.middleware.ts"
click node_api_token_middleware "https://github.com/sukhrajdev/apixy/blob/main/backend/src/middlewares/Apitoken.middleware.ts"
click node_auth_service "https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/auth.service.ts"
click node_token_provider "https://github.com/sukhrajdev/apixy/blob/main/backend/src/providers/token/token.provider.ts"
click node_user_service "https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/user.service.ts"
click node_api_service "https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/api.service.ts"
click node_google_llm "https://github.com/sukhrajdev/apixy/blob/main/backend/src/providers/llm/google/google.llm.ts"
click node_email_provider "https://github.com/sukhrajdev/apixy/blob/main/backend/src/providers/email/email.provider.ts"
click node_prisma_client "https://github.com/sukhrajdev/apixy/blob/main/backend/src/configs/prisma.config.ts"
click node_schema "https://github.com/sukhrajdev/apixy/blob/main/backend/prisma/schema.prisma"
click node_sdk_index "https://github.com/sukhrajdev/apixy/blob/main/sdk/src/index.ts"
click node_sdk_request "https://github.com/sukhrajdev/apixy/blob/main/sdk/src/core/request.core.ts"
click node_sdk_chat "https://github.com/sukhrajdev/apixy/blob/main/sdk/src/core/chat.core.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server,node_routes,node_auth_controller,node_user_controller,node_api_controller,node_validation,node_auth_middleware,node_api_token_middleware,node_auth_service,node_token_provider,node_user_service,node_api_service,node_google_llm,node_email_provider toneBlue
class node_prisma_client,node_schema,node_postgres,node_gmail toneAmber
class node_sdk_index,node_sdk_request,node_sdk_chat toneMint
```

## Components

### Backend API

Express service that owns identity, credentials, and LLM proxying.

- [Express server](https://github.com/sukhrajdev/apixy/blob/main/backend/src/server.ts): Node entry point · `backend/src/server.ts`
- [Public routes](https://github.com/sukhrajdev/apixy/blob/main/backend/src/routes/api.route.ts): HTTP routing · Auth, refresh, user, and provider-key/LLM endpoints. · `backend/src/routes/api.route.ts`
- [Auth controller](https://github.com/sukhrajdev/apixy/blob/main/backend/src/controllers/auth.controller.ts): HTTP controller · `backend/src/controllers/auth.controller.ts`
- [User controller](https://github.com/sukhrajdev/apixy/blob/main/backend/src/controllers/user.controller.ts): HTTP controller · `backend/src/controllers/user.controller.ts`
- [API & LLM controller](https://github.com/sukhrajdev/apixy/blob/main/backend/src/controllers/api.controller.ts): HTTP controller · `backend/src/controllers/api.controller.ts`
- [User schemas](https://github.com/sukhrajdev/apixy/blob/main/backend/src/utils/zod/schema/user.schema.ts): Zod validation · Validates untrusted user input at the HTTP boundary. · `backend/src/utils/zod/schema/user.schema.ts`
- [Bearer auth](https://github.com/sukhrajdev/apixy/blob/main/backend/src/middlewares/auth.middleware.ts): Express middleware · `backend/src/middlewares/auth.middleware.ts`
- [Stored API-token auth](https://github.com/sukhrajdev/apixy/blob/main/backend/src/middlewares/Apitoken.middleware.ts): Express middleware · Resolves and authorizes a saved provider credential token. · `backend/src/middlewares/Apitoken.middleware.ts`
- [Auth service](https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/auth.service.ts): Application service · `backend/src/services/auth.service.ts`
- [JWT provider](https://github.com/sukhrajdev/apixy/blob/main/backend/src/providers/token/token.provider.ts): Token provider · Issues and verifies access and refresh tokens. · `backend/src/providers/token/token.provider.ts`
- [User service](https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/user.service.ts): Application service · `backend/src/services/user.service.ts`
- [Provider-key & LLM service](https://github.com/sukhrajdev/apixy/blob/main/backend/src/services/api.service.ts): Application service · Manages user-owned provider keys and orchestrates LLM calls. · `backend/src/services/api.service.ts`
- [Google Gemini adapter](https://github.com/sukhrajdev/apixy/blob/main/backend/src/providers/llm/google/google.llm.ts): LLM provider adapter · Only materially implemented runtime LLM provider. · `backend/src/providers/llm/google/google.llm.ts`
- [Verification email](https://github.com/sukhrajdev/apixy/blob/main/backend/src/providers/email/email.provider.ts): Email provider · `backend/src/providers/email/email.provider.ts`

### Data & external services

- [Prisma client](https://github.com/sukhrajdev/apixy/blob/main/backend/src/configs/prisma.config.ts): Database access · `backend/src/configs/prisma.config.ts`
- [Relational schema](https://github.com/sukhrajdev/apixy/blob/main/backend/prisma/schema.prisma): Prisma schema · Defines user and user-owned API credential records. · `backend/prisma/schema.prisma`
- **PostgreSQL**: Database
- **Gmail SMTP**: External email service

### TypeScript SDK

Client package for consuming the backend API.

- [SDK public API](https://github.com/sukhrajdev/apixy/blob/main/sdk/src/index.ts): TypeScript package export · `sdk/src/index.ts`
- [SDK request transport](https://github.com/sukhrajdev/apixy/blob/main/sdk/src/core/request.core.ts): SDK core · `sdk/src/core/request.core.ts`
- [SDK chat API](https://github.com/sukhrajdev/apixy/blob/main/sdk/src/core/chat.core.ts): SDK core · `sdk/src/core/chat.core.ts`

## Connections

- SDK public API → SDK request transport: exports
- SDK public API → SDK chat API: exports
- SDK chat API → SDK request transport: uses transport
- SDK request transport → Express server: HTTP requests
- Express server → Public routes: mounts
- Public routes → User schemas: validates input
- Public routes → Bearer auth: protects user routes
- Public routes → Stored API-token auth: guards LLM calls
- Public routes → Auth controller: auth endpoints
- Public routes → User controller: user endpoints
- Public routes → API & LLM controller: key and LLM endpoints
- Auth controller → Auth service: auth operations
- Auth service → JWT provider: issues tokens
- Bearer auth → JWT provider: verifies bearer JWT
- Auth service → Verification email: sends verification
- Verification email → Gmail SMTP: SMTP
- User controller → User service: user lifecycle
- API & LLM controller → Provider-key & LLM service: credential and invocation
- Stored API-token auth → Provider-key & LLM service: authorizes skKey
- Provider-key & LLM service → Google Gemini adapter: Gemini request
- Auth service → Prisma client: account state
- User service → Prisma client: user records
- Provider-key & LLM service → Prisma client: provider credentials
- Prisma client → Relational schema: maps model
- Prisma client → PostgreSQL: queries
