# vtex/node-vtex-api architecture

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

- Interactive diagram: https://gitdiagram.com/vtex/node-vtex-api
- Repository: https://github.com/vtex/node-vtex-api

## Overview

1. **What this repo is**
   - `@vtex/api` is a TypeScript framework/library for building VTEX IO Node services.
   - It sits between an app’s route handlers and VTEX’s platform APIs, providing the runtime, client abstractions, caching, tracing, metrics, and logging that most services need.

2. **Primary integration surface**
   - The main consumer-facing pattern is: create a `Service` and pass route handlers plus client configuration.
   - That `Service` object is the orchestration boundary for an app: it wires incoming requests into the worker runtime, injects `ctx.clients`, and applies common middleware concerns.

3. **Service runtime and request flow**
   - The `src/service/worker/runtime` subtree is the request pipeline implementation.
   - It has separate flows for:
     - **HTTP routes**: authentication, settings, rate limiting, request stats, timing, cookie handling, and error handling.
     - **GraphQL**: schema loading, query execution, context setup, response shaping, uploads, schema updates, and GraphQL-specific error handling.
     - **Built-in handlers/events**: lower-level event and built-in runtime support for platform-driven execution paths.
   - This is the core execution boundary where raw platform requests become a `Context` and then flow into app code.

4. **Client layer: how app code talks to VTEX services**
   - `src/clients` defines the client abstraction used inside handlers via `ctx.clients`.
   - There are three main families:
     - **IO clients** (`IOClient`, `IOClients`, `IOGraphQLClient`) as the base abstractions.
     - **App clients** for VTEX app-scoped APIs.
     - **External clients** for non-platform integrations.
     - **Infra clients** for VTEX infrastructure APIs.
   - This layer keeps handler code thin by pushing remote calls and retries/caching/metrics into reusable client classes.

5. **HTTP transport foundation**
   - `src/HttpClient` is the lowest-level network layer and is built around Axios.
   - It includes request setup, agent management, middleware composition, and GraphQL support.
   - This layer is responsible for cross-cutting transport behavior like cancellation, inflight deduplication, memoization, not-found handling, metrics, recording, and tracing.

6. **Caching architecture**
   - `src/caches` provides multiple cache implementations and a composition layer.
   - The cache stack includes in-memory LRU, disk-backed cache, and multilayer combinations.
   - The important architectural point is that caching is not a single mechanism; the library supports layered cache behavior so clients can reuse responses across requests and process restarts where appropriate.

7. **Observability is first-class**
   - Tracing, metrics, and logging are built into the transport and service runtime.
   - `src/tracing` and `src/service/tracing` manage span creation, error reporting, and request-level tracing middleware.
   - `src/metrics` and `src/service/metrics` collect diagnostics and request metrics, including host/event-loop related measurements.
   - `src/service/logger` centralizes log formatting and metrics-aware logging.
   - Net effect: most request/client paths emit telemetry without app code needing to instrument every call manually.

8. **GraphQL support is a dedicated subsystem**
   - The GraphQL runtime under `src/service/worker/runtime/graphql` handles schema assembly and request execution.
   - It includes custom schema directives such as auth, cache control, deprecated, sanitize, settings, smart cache, and translation-related directives.
   - This indicates the library is not just a transport helper; it also provides opinionated GraphQL execution semantics for VTEX services.

9. **VTEX platform and domain clients**
   - The repo includes concrete client modules for VTEX-specific domains:
     - **Infra**: Apps, Assets, Events, Registry, Router, Workspaces, VBase, Housekeeper, Sphinx, BillingMetrics.
     - **Janus**: tenant/session/license/segment-related APIs.
     - **Catalog GraphQL**: brand, category, product, sku APIs.
     - **External**: Masterdata, PaymentProvider, ID.
   - These are the main outbound dependencies this library is designed to broker.

10. **Errors and request semantics**
   - `src/errors` and `src/utils` define the repository’s shared error taxonomy and response/request helpers.
   - There are specific error types for auth, forbidden, not found, cancellation, rate limits, user input, resolver issues, and GraphQL-specific failures.
   - This standardization is important for architecture because it normalizes how failures propagate from client calls through the service runtime to HTTP/GraphQL responses.

11. **Utilities and runtime helpers**
   - `src/service/worker/runtime/utils` and top-level `src/utils` contain the supporting glue for context composition, retry/cancel logic, tenant/domain/binding helpers, status handling, JSON/message utilities, and conflict resolution.
   - These are not the main architecture, but they define the shared behavioral contract used by the clients and runtime pipelines.

12. **Repo support structure**
   - `docs/` documents metrics, tracing, and examples, showing the library’s emphasis on operational correctness.
   - `__mocks__`, tests, and `gen/manifest.schema` indicate a strongly tested package with generated schema artifacts.
   - `.github/workflows/publish-npm.yml` and the release scripts show this is published as a reusable npm package rather than deployed as a standalone service.

13. **How to draw the architecture**
   - A good diagram would place **app code** at the top, calling into **Service** and **ctx.clients**.
   - Beneath that, separate the **request runtime** (HTTP/GraphQL/event pipelines) from the **client/transport layer**.
   - Under the transport layer, show **caching**, **Axios/request setup**, and **observability** as shared cross-cutting subsystems.
   - On the outbound side, show the VTEX platform APIs and external services the concrete clients target.

Main entry points: [`src/HttpClient/HttpClient.ts`](https://github.com/vtex/node-vtex-api/blob/master/src/HttpClient/HttpClient.ts) (HttpClient), [`src/clients`](https://github.com/vtex/node-vtex-api/tree/master/src/clients) (ctx.clients), [`src/service/worker/runtime/Service.ts`](https://github.com/vtex/node-vtex-api/blob/master/src/service/worker/runtime/Service.ts) (Service), [`src/service/worker/runtime/http`](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/http) (HTTP runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["Consumers & APIs"]
  node_app["App code<br/>consumer"]
  node_errors["Errors<br/>error taxonomy"]
  node_utils["Utils<br/>shared helpers"]
end

subgraph group_runtime["Service runtime"]
  node_service["Service<br/>orchestrator<br/>[Service.ts]"]
  node_http_runtime["HTTP runtime<br/>request pipeline"]
  node_graphql_runtime["GraphQL runtime<br/>graphql pipeline"]
  node_builtins["Built-ins<br/>platform runtime"]
  node_events["Events<br/>event pipeline"]
  node_http_router["HTTP router<br/>[router.ts]"]
  node_http_mw["HTTP middleware<br/>middleware stack"]
  node_gql_schema["Schema"]
  node_gql_mw["GraphQL middleware<br/>middleware stack"]
end

subgraph group_clients["Client layer"]
  node_clients_root["ctx.clients<br/>client registry"]
  node_io_clients["IO clients<br/>base clients<br/>[IOClient.ts]"]
  node_app_clients["App clients"]
  node_infra_clients["Infra clients<br/>platform clients"]
  node_janus_clients["Janus clients<br/>platform clients"]
  node_external_clients["External clients<br/>third-party clients"]
  node_catalog_gql["Catalog GraphQL<br/>domain clients"]
end

subgraph group_transport["Transport & cache"]
  node_http_client["HttpClient<br/>transport core<br/>[HttpClient.ts]"]
  node_cache_layer["Caching<br/>cache stack"]
  node_request_setup["Request setup<br/>axios setup"]
end

subgraph group_observability["Observability"]
  node_tracing["Tracing<br/>observability"]
  node_metrics["Metrics<br/>observability"]
  node_logger["Logger<br/>observability"]
end

node_app -->|"creates"| node_service
node_service -->|"serves"| node_http_runtime
node_service -->|"serves"| node_graphql_runtime
node_service -->|"includes"| node_builtins
node_service -->|"includes"| node_events
node_http_runtime -->|"routes"| node_http_router
node_http_runtime -->|"applies"| node_http_mw
node_graphql_runtime -->|"loads"| node_gql_schema
node_graphql_runtime -->|"applies"| node_gql_mw
node_http_runtime -->|"injects"| node_clients_root
node_graphql_runtime -->|"injects"| node_clients_root
node_clients_root -->|"builds on"| node_io_clients
node_clients_root -->|"exposes"| node_app_clients
node_clients_root -->|"exposes"| node_infra_clients
node_clients_root -->|"exposes"| node_janus_clients
node_clients_root -->|"exposes"| node_external_clients
node_clients_root -->|"exposes"| node_catalog_gql
node_io_clients -->|"uses"| node_http_client
node_app_clients -->|"uses"| node_http_client
node_infra_clients -->|"uses"| node_http_client
node_janus_clients -->|"uses"| node_http_client
node_external_clients -->|"uses"| node_http_client
node_catalog_gql -->|"uses"| node_http_client
node_http_client -->|"consults"| node_cache_layer
node_http_client -->|"initializes"| node_request_setup
node_request_setup -->|"emits"| node_tracing
node_http_client -->|"emits"| node_tracing
node_http_client -->|"emits"| node_metrics
node_service -->|"emits"| node_metrics
node_service -->|"logs"| node_logger
node_http_runtime -->|"maps"| node_errors
node_graphql_runtime -->|"maps"| node_errors
node_http_runtime -->|"uses"| node_utils
node_graphql_runtime -->|"uses"| node_utils
node_clients_root -->|"uses"| node_utils

click node_service "https://github.com/vtex/node-vtex-api/blob/master/src/service/worker/runtime/Service.ts"
click node_http_runtime "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/http"
click node_graphql_runtime "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/graphql"
click node_builtins "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/builtIn"
click node_events "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/events"
click node_http_router "https://github.com/vtex/node-vtex-api/blob/master/src/service/worker/runtime/http/router.ts"
click node_http_mw "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/http/middlewares"
click node_gql_schema "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/graphql/schema"
click node_gql_mw "https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/graphql/middlewares"
click node_clients_root "https://github.com/vtex/node-vtex-api/tree/master/src/clients"
click node_io_clients "https://github.com/vtex/node-vtex-api/blob/master/src/clients/IOClient.ts"
click node_app_clients "https://github.com/vtex/node-vtex-api/tree/master/src/clients/apps"
click node_infra_clients "https://github.com/vtex/node-vtex-api/tree/master/src/clients/infra"
click node_janus_clients "https://github.com/vtex/node-vtex-api/tree/master/src/clients/janus"
click node_external_clients "https://github.com/vtex/node-vtex-api/tree/master/src/clients/external"
click node_catalog_gql "https://github.com/vtex/node-vtex-api/tree/master/src/clients/apps/catalogGraphQL"
click node_http_client "https://github.com/vtex/node-vtex-api/blob/master/src/HttpClient/HttpClient.ts"
click node_cache_layer "https://github.com/vtex/node-vtex-api/tree/master/src/caches"
click node_request_setup "https://github.com/vtex/node-vtex-api/tree/master/src/HttpClient/middlewares/request/setupAxios"
click node_tracing "https://github.com/vtex/node-vtex-api/tree/master/src/tracing"
click node_metrics "https://github.com/vtex/node-vtex-api/tree/master/src/metrics"
click node_logger "https://github.com/vtex/node-vtex-api/tree/master/src/service/logger"
click node_errors "https://github.com/vtex/node-vtex-api/tree/master/src/errors"
click node_utils "https://github.com/vtex/node-vtex-api/tree/master/src/utils"

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_app,node_errors,node_utils toneBlue
class node_service,node_http_runtime,node_graphql_runtime,node_builtins,node_events,node_http_router,node_http_mw,node_gql_schema,node_gql_mw toneAmber
class node_clients_root,node_io_clients,node_app_clients,node_infra_clients,node_janus_clients,node_external_clients,node_catalog_gql toneMint
class node_http_client,node_cache_layer,node_request_setup toneRose
class node_tracing,node_metrics,node_logger toneIndigo
```

## Components

### Consumers & APIs

App code and downstream platform/external systems

- **App code**: consumer · VTEX IO app handlers and resolvers built on top of this package
- [Errors](https://github.com/vtex/node-vtex-api/tree/master/src/errors): error taxonomy · Shared auth, forbidden, not found, cancellation, rate limit, user input, resolver, and GraphQL errors · `src/errors`
- [Utils](https://github.com/vtex/node-vtex-api/tree/master/src/utils): shared helpers · Context, tenant, binding, retry, cancel, status, JSON, and conflict helpers · `src/utils`

### Service runtime

HTTP, GraphQL, and event execution pipelines

- [Service](https://github.com/vtex/node-vtex-api/blob/master/src/service/worker/runtime/Service.ts): orchestrator · Main boundary that wires routes, middleware, clients, and runtime context · `src/service/worker/runtime/Service.ts`
- [HTTP runtime](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/http): request pipeline · HTTP route execution with auth, settings, rate limiting, timings, cookies, and errors · `src/service/worker/runtime/http`
- [GraphQL runtime](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/graphql): graphql pipeline · Schema loading, query execution, context setup, response shaping, uploads, and schema updates · `src/service/worker/runtime/graphql`
- [Built-ins](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/builtIn): platform runtime · Built-in handler and event execution paths for platform-driven requests · `src/service/worker/runtime/builtIn`
- [Events](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/events): event pipeline · Event routing and event-specific middleware · `src/service/worker/runtime/events`
- [HTTP router](https://github.com/vtex/node-vtex-api/blob/master/src/service/worker/runtime/http/router.ts): router · Resolves incoming HTTP routes to service handlers · `src/service/worker/runtime/http/router.ts`
- [HTTP middleware](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/http/middlewares): middleware stack · Auth tokens, cancellation, clients, context, error handling, rate limits, stats, timings, and cookies · `src/service/worker/runtime/http/middlewares`
- [Schema](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/graphql/schema): graphql schema · GraphQL schema assembly and type definitions · `src/service/worker/runtime/graphql/schema`
- [GraphQL middleware](https://github.com/vtex/node-vtex-api/tree/master/src/service/worker/runtime/graphql/middlewares): middleware stack · Context, error, query, response, upload, and schema update stages · `src/service/worker/runtime/graphql/middlewares`

### Client layer

Context clients and domain-specific client families

- [ctx.clients](https://github.com/vtex/node-vtex-api/tree/master/src/clients): client registry · Request-scoped client container exposed to handlers · `src/clients`
- [IO clients](https://github.com/vtex/node-vtex-api/blob/master/src/clients/IOClient.ts): base clients · Core client abstractions including IOClient, IOClients, and IOGraphQLClient · `src/clients/IOClient.ts`
- [App clients](https://github.com/vtex/node-vtex-api/tree/master/src/clients/apps): vt-ex app clients · App-scoped clients such as billing, builder, settings, and messages · `src/clients/apps`
- [Infra clients](https://github.com/vtex/node-vtex-api/tree/master/src/clients/infra): platform clients · VTEX infrastructure clients for apps, assets, events, registry, router, workspaces, VBase, housekeeper, Sphinx, and billing metrics · `src/clients/infra`
- [Janus clients](https://github.com/vtex/node-vtex-api/tree/master/src/clients/janus): platform clients · Tenant, session, license, and segment clients · `src/clients/janus`
- [External clients](https://github.com/vtex/node-vtex-api/tree/master/src/clients/external): third-party clients · Non-platform integrations such as masterdata, payment provider, and ID · `src/clients/external`
- [Catalog GraphQL](https://github.com/vtex/node-vtex-api/tree/master/src/clients/apps/catalogGraphQL): domain clients · Brand, category, product, and sku GraphQL clients · `src/clients/apps/catalogGraphQL`

### Transport & cache

Shared HTTP, caching, and request middleware

- [HttpClient](https://github.com/vtex/node-vtex-api/blob/master/src/HttpClient/HttpClient.ts): transport core · Axios-based request layer with agents, request setup, GraphQL support, and transport middleware · `src/HttpClient/HttpClient.ts`
- [Caching](https://github.com/vtex/node-vtex-api/tree/master/src/caches): cache stack · In-memory, disk-backed, and multilayer cache implementations · `src/caches`
- [Request setup](https://github.com/vtex/node-vtex-api/tree/master/src/HttpClient/middlewares/request/setupAxios): axios setup · Agent management, Axios initialization, interceptors, retries, cancellation, and tracing hooks · `src/HttpClient/middlewares/request/setupAxios`

### Observability

Tracing, metrics, and logging subsystems

- [Tracing](https://github.com/vtex/node-vtex-api/tree/master/src/tracing): observability · Span creation, error reporting, and request-level tracing · `src/tracing`
- [Metrics](https://github.com/vtex/node-vtex-api/tree/master/src/metrics): observability · Diagnostics metrics, request metrics, and host/event-loop measurements · `src/metrics`
- [Logger](https://github.com/vtex/node-vtex-api/tree/master/src/service/logger): observability · Centralized log formatting and metrics-aware logging · `src/service/logger`

## Connections

- App code → Service: creates · App code instantiates the service boundary
- Service → HTTP runtime: serves · Service routes HTTP requests through the HTTP runtime
- Service → GraphQL runtime: serves · Service routes GraphQL requests through the GraphQL runtime
- Service → Built-ins: includes · Service exposes built-in runtime execution paths
- Service → Events: includes · Service exposes event handling flow
- HTTP runtime → HTTP router: routes · HTTP runtime dispatches requests through routing
- HTTP runtime → HTTP middleware: applies · HTTP runtime composes request middleware
- GraphQL runtime → Schema: loads · GraphQL runtime assembles schema and type definitions
- GraphQL runtime → GraphQL middleware: applies · GraphQL runtime composes execution middleware
- HTTP runtime → ctx.clients: injects · Request context is populated with clients
- GraphQL runtime → ctx.clients: injects · GraphQL context also receives clients
- ctx.clients → IO clients: builds on · Client registry is based on IO client abstractions
- ctx.clients → App clients: exposes · App-scoped client family is surfaced through ctx.clients
- ctx.clients → Infra clients: exposes · Infrastructure client family is surfaced through ctx.clients
- ctx.clients → Janus clients: exposes · Janus client family is surfaced through ctx.clients
- ctx.clients → External clients: exposes · External integration clients are surfaced through ctx.clients
- ctx.clients → Catalog GraphQL: exposes · Catalog GraphQL client family is surfaced through ctx.clients
- IO clients → HttpClient: uses · Base clients execute network calls through HttpClient
- App clients → HttpClient: uses · App clients send requests via the shared transport layer
- Infra clients → HttpClient: uses · Infra clients send requests via the shared transport layer
- Janus clients → HttpClient: uses · Janus clients send requests via the shared transport layer
- External clients → HttpClient: uses · External clients send requests via the shared transport layer
- Catalog GraphQL → HttpClient: uses · Catalog GraphQL clients use the shared transport layer
- HttpClient → Caching: consults · Transport uses layered caching and memoization behavior
- HttpClient → Request setup: initializes · Transport bootstraps Axios, agents, interceptors, and tracing hooks
- Request setup → Tracing: emits · Axios request setup integrates tracing spans
- HttpClient → Tracing: emits · Transport emits tracing and error reporting signals
- HttpClient → Metrics: emits · Transport records diagnostics and request metrics
- Service → Metrics: emits · Runtime request paths collect service-level metrics
- Service → Logger: logs · Runtime uses centralized logging
- HTTP runtime → Errors: maps · HTTP errors are normalized into shared error types
- GraphQL runtime → Errors: maps · GraphQL failures are normalized into shared error types
- HTTP runtime → Utils: uses · HTTP runtime relies on shared request and context helpers
- GraphQL runtime → Utils: uses · GraphQL runtime relies on shared request and context helpers
- ctx.clients → Utils: uses · Client behavior depends on shared helper utilities
