Skip to content

Vite Plugin

The @shiftapi/vite-plugin starts your Go server, proxies API requests, and regenerates TypeScript types on every save.

Terminal window
npm install shiftapi @shiftapi/vite-plugin

Add the plugin to your Vite config:

vite.config.ts
import { defineConfig } from "vite";
import { shiftapi } from "@shiftapi/vite-plugin";
export default defineConfig({
plugins: [shiftapi()],
});

Then create a shiftapi.config.ts in your project root:

shiftapi.config.ts
import { defineConfig } from "shiftapi";
export default defineConfig({
server: "./server/main.go",
});
OptionDefaultDescription
serverPath to your Go server entry point (required)
url"http://localhost:8080"Address your Go server listens on
baseUrl"/"Base URL for the generated client
  1. Starts your Go server during development
  2. Proxies API requests to the Go server
  3. Fetches the OpenAPI spec from /openapi.json
  4. Generates a fully-typed TypeScript client in .shiftapi/
import { client } from "@shiftapi/client";
const { data } = await client.POST("/hello", {
body: { name: "World" },
});
console.log(data.message);
// ^? (property) message: string

Types are regenerated automatically when you save a Go file — changes appear in your editor via Vite’s HMR.

Wrap the generated client with openapi-react-query to get typed useQuery and useMutation hooks:

Terminal window
npm install @tanstack/react-query openapi-react-query
import createClient from "openapi-react-query";
import { client } from "@shiftapi/client";
export const api = createClient(client);
const health = api.useQuery("get", "/health");
const greet = api.useMutation("post", "/greet");

The generated client includes an sse function for SSE endpoints registered with HandleSSE. It returns a typed async iterable:

import { sse } from "@shiftapi/client";
const stream = sse("/events");
for await (const event of stream) {
console.log(event); // fully typed from your Go Event struct
}

See Server-Sent Events for full details.

The generated client also includes a websocket function for WebSocket endpoints registered with HandleWS. It returns a typed bidirectional connection:

import { websocket } from "@shiftapi/client";
const ws = websocket("/chat");
ws.send({ text: "hello" });
for await (const msg of ws) {
console.log(msg.text); // fully typed from your Go Send struct
}

See WebSockets for full details.