{"id":18435,"library":"http-schemas","title":"http-schemas","description":"Strongly-typed HTTP schemas for TypeScript that enforce API contracts at build time and runtime. Version 0.13.2 ships TypeScript types and integrates with the rtti library for schema specification and enforcement. It supports both client-side (createHttpClient) and server-side (Express integration with createRequestHandler and decorateExpressRouter) usage. Key differentiators include static type checking of request/response payloads, runtime validation, and automatic response trimming to prevent information leaks. Release cadence is irregular; the v0.10 API is deprecated but still supported. Alternatives like tRPC and Zod offer similar functionality but with different trade-offs.","status":"active","version":"0.13.2","language":"javascript","source_language":"en","source_url":"https://github.com/yortus/http-schemas","tags":["javascript","typescript"],"install":[{"cmd":"npm install http-schemas","lang":"bash","label":"npm"},{"cmd":"yarn add http-schemas","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-schemas","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for schema definition and runtime validation.","package":"rtti","optional":false}],"imports":[{"note":"createHttpSchema is a named export from the main package.","wrong":"import httpSchemas from 'http-schemas'","symbol":"createHttpSchema","correct":"import { createHttpSchema } from 'http-schemas'"},{"note":"createHttpClient is exported from the client subpath, not the main entry.","wrong":"import { createHttpClient } from 'http-schemas'","symbol":"createHttpClient","correct":"import { createHttpClient } from 'http-schemas/client'"},{"note":"In CommonJS, use require but ensure it's from the server subpath.","wrong":"const createRequestHandler = require('http-schemas/server').createRequestHandler","symbol":"createRequestHandler","correct":"import { createRequestHandler } from 'http-schemas/server'"},{"note":"decorateExpressRouter is exported from the server subpath.","wrong":"import { decorateExpressRouter } from 'http-schemas'","symbol":"decorateExpressRouter","correct":"import { decorateExpressRouter } from 'http-schemas/server'"},{"note":"t is re-exported from http-schemas for convenience when defining schemas.","wrong":"import { t } from 'rtti'","symbol":"t","correct":"import { t } from 'http-schemas'"}],"quickstart":{"code":"import { createHttpSchema, t } from 'http-schemas';\nimport { createHttpClient } from 'http-schemas/client';\n\n// Define a shared schema\nconst apiSchema = createHttpSchema({\n  'POST /sum': {\n    requestBody: t.array(t.number),\n    responseBody: t.number,\n  },\n  'GET /greet/:name': {\n    responseBody: t.string,\n  },\n});\n\n// Create a client\nconst client = createHttpClient(apiSchema, { baseURL: 'https://api.example.com' });\n\n// Use the client\nasync function main() {\n  const sum = await client.post('/sum', { body: [1, 2, 3] });\n  console.log(sum); // 6\n  const greeting = await client.get('/greet/:name', { params: { name: 'World' } });\n  console.log(greeting); // Hello, World!\n}\nmain().catch(console.error);","lang":"typescript","description":"Demonstrates defining an HTTP schema with createHttpSchema and using createHttpClient to make typed requests."},"warnings":[{"fix":"Migrate to the v0.11+ API: use createHttpSchema instead of the old API.","message":"The v0.10 API is deprecated but still supported.","severity":"deprecated","affected_versions":">=0.11.0"},{"fix":"Be aware that response objects will have only the properties defined in the schema.","message":"Response payloads are trimmed of excess properties at runtime to prevent information leaks.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Update server code to use decorateExpressRouter and createRequestHandler as shown in the README.","message":"The v0.10 API for server-side code is deprecated; decorateExpressRouter and createRequestHandler are the new way.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Ensure that requestBody and responseBody use valid `t` types (e.g., t.number, t.string, t.array, t.object).","message":"The schema definition uses the `t` object from `rtti`. Incorrect usage can lead to runtime validation failures.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always provide a baseURL option when creating a client, or ensure the schema includes full URLs.","message":"createHttpClient instances are cheap to create but require a baseURL; missing baseURL will cause runtime errors.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Wrap API calls in try/catch and handle validation errors from http-schemas.","cause":"The request body is undefined or not the expected type because the schema validation failed but error handling is missing.","error":"TypeError: Cannot read properties of undefined (reading 'reduce')"},{"fix":"Ensure the body matches the schema: client.post('/sum', { body: [1,2] }).","cause":"The request body sent to a POST /sum endpoint is not an array of numbers.","error":"Error: Schema validation failed: request body is not an array"},{"fix":"Correct the type of the argument to match the schema definition.","cause":"The static type checking from http-schemas catches mismatches between schema and usage.","error":"TS2322: Type 'string' is not assignable to type 'number'"},{"fix":"Use import { createHttpClient } from 'http-schemas/client'.","cause":"Import path is incorrect; http-schemas uses subpath exports.","error":"Error: Cannot find module 'http-schemas/client'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}