Sharing with Navios Server
A declaration describes an endpoint without saying which side of the wire you are on. The client calls it; a Navios server implements it. When both import the same declaration, a change to the contract is a compile error on whichever side has not caught up.
This page shows how to arrange that. The server's own behavior is documented in Controllers and handlers.
Put declarations where both sides can import them
Declarations need a home that neither the client nor the server owns: a workspace package in a monorepo, or a shared folder.
The contract creates its builder without a client, because only the calling side needs one.
On the client
The client app supplies the HTTP client once, at start-up, and then calls the endpoints.
On the server
A controller method is bound to a declaration with @Endpoint. The route, the validation and the handler's types all come from it.
The server never calls the endpoint function, so it does not need an HTTP client. It reads getUser.config.
HandlerParams<typeof getUser> is the server-side counterpart of Builder's EndpointParams: it describes what a handler receives, where EndpointParams describes what a caller passes. The handler's return value is checked too. Returning a user that does not match userSchema, or reply() with a status the declaration does not list, is a compile error.
What each side sees
The same schemas produce different types on each side, because each side is on a different end of the validation.
In practice: a query schema with page: z.number().default(1) lets the client omit page, and the handler always receives a number.
Both sides validate. The client checks its input before sending and the response after receiving; the server checks the incoming request before your handler runs. A request built by something other than your client is still held to the contract.
Requests: the input travels
The client validates what you pass and then sends it unchanged. Defaults, transforms and the removal of unknown keys happen on the server only, so each of them runs once. A transform may therefore turn its input into anything: the handler receives its output.
builder({ send: 'output' }) sends what the schemas produced instead, and is meant for other APIs. With a Navios server it would apply every transform twice. What is sent compares the two.
Query strings, URL parameters and form fields
They travel as text. The server decodes the text with the schema's JSON Schema before it validates, so a contract declares the types the handler wants: z.number(), z.int(), z.boolean(), z.array(...) and z.object(...) in a querySchema, a urlParamsSchema or a multipart requestSchema need no coercion, and the client is typed with them.
This works with schema libraries that implement Standard JSON Schema, as Zod 4 does. With a library that does not, describe the text itself. Never use z.coerce.boolean(): it turns "false" into true.
Responses: the output travels
The server sends the output of the response schema, which is how fields the contract does not mention are removed, and the client parses that JSON with the same schema again. A responseSchema, an entry of responses or errorSchema, and the payloadSchema of an event must therefore accept their own output as JSON. Responses in a shared contract shows what that allows.
With validateResponse: false the client receives what the server sent, the schema's output in JSON form, while the type is the schema's input. Use it only with a schema for which the two are the same.
Errors
A Navios server reports its own failures, such as a guard that denies access or input that fails validation, as problem details: JSON with the content type application/problem+json. Describe them in errorSchema like any other error body:
Declarations that are not plain JSON
@Endpoint implements every kind of declaration. What changes with body and response is what the handler receives and returns:
A handler that returns the wrong thing for its declaration is a type error.
Keep the contract free of side concerns
A contract that both sides import should contain only what both sides agree on:
- No client. The builder is created bare; each consumer wires its own.
- No environment access. Base URLs and tokens belong to the app that makes the calls.
- No browser-only or server-only imports, or one side will fail to build.
z.instanceof(File)in an upload schema is fine on runtimes that defineFile, which includes current Node.js and Bun.
Organizing an API covers how to lay out the declarations themselves.