Send JSON and get a typed result back
POST a validated body and read a typed, runtime-validated result — input checked before the request, output checked after.
Task
You are POSTing JSON to an API and want the result back as a typed value — and
you would like bad input rejected before it leaves the process, not returned to
you as a 400 from the server.
Example
One way: validate the body going out and the response coming back, and type the
result with the stitch<T> generic.
import { } from 'stitchapi';
import { } from 'zod';
const = <{ : number; : string }>({
: 'POST',
: 'https://api.example.com',
: '/users',
: { : .({ : .() }) },
: .({ : .(), : .() }),
});
// A bad body throws before the POST; a bad response throws after.
const = await ({ : { : 'Ada' } });user is typed { id: number; name: string }.
How it works
input.body is checked before the request — wrong input fails fast with no
network call. output is checked after the response returns. Either failure
throws STITCH_VALIDATION. The stitch<T>
generic types the resolved value, and the config takes the z.object(...)
schema directly. Any Standard
Schema validator (Valibot, ArkType)
works the same way, passed straight to input/output.
See also
Recipes
Working examples of common tasks — each one the whole thing in a single block, with links down to the guides for depth.
Define an entity once, derive every request shape
Declare a resource schema once and derive the create body, update body, and query filter with your validator’s own .omit()/.partial()/.pick() — no StitchAPI-specific schema layer.