How vtex/node-vtex-api works
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: HttpClient.ts HttpClient · clients ctx.clients · Service.ts Service · http HTTP runtime
Components 25
Consumers & APIs
App code and downstream platform/external systems
Service runtime
HTTP, GraphQL, and event execution pipelines
- Service · Main boundary that wires routes, middleware, clients, and runtime context
src/service/worker/runtime/Service.ts - HTTP runtime · HTTP route execution with auth, settings, rate limiting, timings, cookies, and errors
src/service/worker/runtime/http - GraphQL runtime · Schema loading, query execution, context setup, response shaping, uploads, and schema updates
src/service/worker/runtime/graphql - Built-ins · Built-in handler and event execution paths for platform-driven requests
src/service/worker/runtime/builtIn - Events · Event routing and event-specific middleware
src/service/worker/runtime/events - HTTP router · Resolves incoming HTTP routes to service handlers
src/service/worker/runtime/http/router.ts - HTTP middleware · Auth tokens, cancellation, clients, context, error handling, rate limits, stats, timings, and cookies
src/service/worker/runtime/http/middlewares - Schema · GraphQL schema assembly and type definitions
src/service/worker/runtime/graphql/schema - GraphQL middleware · 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 · Request-scoped client container exposed to handlers
src/clients - IO clients · Core client abstractions including IOClient, IOClients, and IOGraphQLClient
src/clients/IOClient.ts - App clients · App-scoped clients such as billing, builder, settings, and messages
src/clients/apps - Infra clients · VTEX infrastructure clients for apps, assets, events, registry, router, workspaces, VBase, housekeeper, Sphinx, and billing metrics
src/clients/infra - Janus clients · Tenant, session, license, and segment clients
src/clients/janus - External clients · Non-platform integrations such as masterdata, payment provider, and ID
src/clients/external - Catalog GraphQL · Brand, category, product, and sku GraphQL clients
src/clients/apps/catalogGraphQL
Transport & cache
Shared HTTP, caching, and request middleware
- HttpClient · Axios-based request layer with agents, request setup, GraphQL support, and transport middleware
src/HttpClient/HttpClient.ts - Caching · In-memory, disk-backed, and multilayer cache implementations
src/caches - Request setup · Agent management, Axios initialization, interceptors, retries, cancellation, and tracing hooks
src/HttpClient/middlewares/request/setupAxios
Observability
Tracing, metrics, and logging subsystems
Connections 35
- 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