Skip to content

Creating clients ​

Generating a typed client for your API from the command line.

package @smitejs/client

@smitejs/client turns your server definition into a typed, builder-style client. Every route you declare becomes a nested object with $method calls; params come from the path template and are typed. There is no runtime codegen — the client is generated once, at build time, and the emitted module is plain TypeScript over a small fetch layer.

The quick way: smite generate client ​

With the CLI installed, add a smite.config.ts to your project that lists the client() plugin:

ts
const config = defineSmiteConfig({
  entries: ["./src/app.ts", "./src/handlers/events.ts"],
  plugins: [client({ outfile: "./src/app.client.ts" })],
});
ts

[
  "./src/app.ts",
  "./src/handlers/events.ts",
]
"client"

Use entries to compile several serverless handler entries; the client merges the routes from every app into one builder.

Then run smite generate client.

The CLI compiles your app in collect mode, runs it once to discover the declared routes, and writes the client to outfile. Each entry must call http.app() at module scope.

The same flow, from code ​

If you do not want the config file, the engine is exported directly. This is exactly what the CLI runs:

ts
await dispatch(config.plugins, "client", { apps });
ts
"users"

The generated client ​

The emitted module exports configure() and an api builder that mirrors your routes:

ts
const code = await generate(options);
ts
"users"
"$id"

Call it like any typed function — configure sets the defaults, and each $method takes a bucket with required params:

ts
configure({
  baseUrl: "https://api.example.com",
  fetch: async (url: string, init: RequestInit) => {
    calls.push({ url, method: init.method });
    return new Response(JSON.stringify({ id: "42" }), { status: 200 });
  },
});

const response = await api.users.$id.$get({ params: { id: "42" } });
ts
"https://api.example.com/users/42"
"GET"
200
{ id: "42" }

What you get ​

  • One $method leaf per endpoint; dynamic :param segments become $param namespaces with required, typed params.
  • query, headers, and body are loose optional buckets; $config overrides configure() per call.
  • Responses are { status, body, headers } and never throw on non-2xx.
  • The generated file imports only @smitejs/client/runtime — no registry, no framework.

See also ​

  • generated-api for the builder shape in detail.
  • @smitejs/openapi's "Exposing OpenAPI" to serve a spec for the same app.

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/techniques/client-creating