Skip to content

Client project setup ​

Your frontends talk to your Fusion Stack API through a small typed client that you generate from the API's OpenAPI document. You never write or publish a full SDK: the generated file contains only types and endpoint definitions, and imports the shared runtime, @fusion-stack/client.

The workflow is always the same:

  1. Build the API with fusion-stack/laravel.
  2. Export its contract: php artisan api:openapi --output=openapi.json.
  3. Generate the client: npx @fusion-stack/client generate openapi.json --out api.
  4. Use it in Nuxt with @fusion-stack/nuxt, or directly in React Native or Node.

What changes between projects is where the generated client lives and how frontends receive it. Pick one of the distribution options below.

Calling the API from another language, or without a generated client? See Calling the API.

Generate the client ​

sh
php artisan api:openapi --output=openapi.json
npx @fusion-stack/client generate openapi.json --out api

This writes api/index.ts, which contains:

  • An interface per resource, flattened the way the client returns it: id, type, the attributes, and each relationship as the related resource (order.customer?.full_name).
  • A typed endpoint per operation. Path parameters, the query (allowed includes, fields, filters with their operators, sorts, pagination style), the request body and the response are all checked at compile time.
  • version: the API version the document describes. The client sends it as Api-Version on every request.

The generator also takes a URL, such as the API's docs route:

sh
npx @fusion-stack/client generate https://api.example.com/docs/openapi.json --out api

Commit the generated file. Its output depends only on the OpenAPI document, so regenerating gives a clean diff: a pull request that changes the API shows exactly what changed for clients. It is also worth committing openapi.json itself, for the same reason.

Versions ​

The generated client pins the API version it was generated from. Nothing the API team does later changes the shape your app receives, until you choose to upgrade.

To upgrade when the API releases a new version:

  1. Read what changed: php artisan api:changelog, or the changelog on the docs route.
  2. Regenerate from the new spec. version moves to the new date and the types change with it.
  3. Fix the type errors: they are the breaking changes that affect your code.
  4. Ship. Apps already in users' hands keep sending their old version, and the API keeps answering in that version's shape until it is retired.

To stay on an older version while generating, ask for that version's spec. Do not pass an old --version with a newer spec, as the types would not match the responses. The generator warns if you do.

sh
php artisan api:openapi --api-version=2026-06-01 --output=openapi.json

Mobile apps should watch for deprecation. Responses for a deprecated version carry Deprecation and Sunset headers, readable from any result as result.headers.get('Sunset'). Report them to your error tracker so you know when old builds need a forced update.

Distribution options ​

Keep the API and its frontends in one repository and generate the client into a workspace package. Nothing is published, and an API change and the frontend change that uses it land in the same pull request.

text
acme/
├─ api/                    Laravel app with fusion-stack/laravel
├─ packages/api-client/    generated client (workspace package)
│  ├─ openapi.json
│  ├─ api/index.ts         generated
│  └─ package.json
├─ web/                    Nuxt app
├─ mobile/                 React Native app
└─ package.json            npm workspaces: packages/*, web, mobile
json
{
  "name": "@acme/api-client",
  "private": true,
  "type": "module",
  "exports": "./api/index.ts",
  "scripts": {
    "openapi": "php ../../api/artisan api:openapi --output=openapi.json",
    "generate": "npm run openapi && fusion-stack-client generate openapi.json"
  },
  "dependencies": {
    "@fusion-stack/client": "^0.3.0"
  }
}

The package exports TypeScript source, which Nuxt (Vite) and React Native (Metro) compile directly. The apps depend on it as "@acme/api-client": "*" and import from it:

ts
import { getV1Orders, version } from '@acme/api-client';

Add a check to the API's CI so nobody forgets to regenerate. It fails when the committed client no longer matches the routes:

yaml
- run: npm run generate --workspace @acme/api-client
- run: git diff --exit-code packages/api-client

Separate repositories ​

When the frontends live in their own repositories, the API repository builds the client and publishes it to a private registry such as GitHub Packages. Frontends depend on it like any other package and upgrade by bumping it, with Renovate or Dependabot opening the pull requests.

Keep the package in the API repository, say in api-client/. Unlike the monorepo version, it is compiled before publishing:

json
{
  "name": "@acme/api-client",
  "version": "0.0.0",
  "type": "module",
  "exports": {
    ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" }
  },
  "files": ["dist", "openapi.json"],
  "publishConfig": { "registry": "https://npm.pkg.github.com" },
  "scripts": {
    "build": "fusion-stack-client generate openapi.json --out src && tsc"
  },
  "dependencies": {
    "@fusion-stack/client": "^0.3.0"
  },
  "devDependencies": {
    "typescript": "^7.0.2"
  }
}
json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "declaration": true,
    "skipLibCheck": true,
    "rootDir": "src",
    "outDir": "dist"
  },
  "include": ["src"]
}

Publish it whenever you release the API. Tag API releases v1.4.0 and the client gets the same version:

yaml
# .github/workflows/api-client.yml
name: Publish API client

on:
  release:
    types: [published]

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v7
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.4'
      - uses: ramsey/composer-install@v4
      - uses: actions/setup-node@v7
        with:
          node-version: 24
          registry-url: https://npm.pkg.github.com
      - run: php artisan api:openapi --output=api-client/openapi.json
      - working-directory: api-client
        run: |
          npm ci
          npm version "${GITHUB_REF_NAME#v}" --no-git-tag-version
          npm run build
          npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Bump the major version of your API releases when the latest API version date changes. Updating the package then also moves frontends to the new API version, and a major bump makes that a deliberate step rather than a routine update.

Third parties ​

Partners and other teams get the contract instead of a client, and generate one in their own language with any OpenAPI generator.

The docs route (/docs by default, api.docs.path in config/api.php) serves everything they need:

URLContents
/docsThe API reference, with a switcher for every supported version
/docs/openapi.json?version=2026-06-01The OpenAPI document for a version; the latest without version
/docs/changelogWhat changed in each version

They should pin the version they generated from by sending it as Api-Version, exactly as the TypeScript client does, so your releases never break them.

Using the client ​

Nuxt ​

Install @fusion-stack/nuxt and pass it the generated version:

ts
// nuxt.config.ts
import { version } from '@acme/api-client';

export default defineNuxtConfig({
  modules: ['@fusion-stack/nuxt'],
  fusionStack: {
    baseUrl: 'https://api.example.com',
    version,
  },
});
vue
<script setup lang="ts">
import { getV1Orders } from '@acme/api-client';

const { data, error } = await useApiData(getV1Orders, {
  query: (q) => q.include('customer').sort('-created_at'),
});
</script>

useApiData() renders on the server and hydrates without refetching. useApi() gives you the client for mutations, and useApiToken() holds the signed-in user's token. See the module's README for the options, including a private serverBaseUrl for server-side requests and proxying the API through your site to avoid CORS.

React Native ​

React Native uses the runtime directly. AsyncStorage works as the ETag cache's storage as it is:

ts
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as SecureStore from 'expo-secure-store';
import { Client } from '@fusion-stack/client';
import { getV1Orders, version } from '@acme/api-client';

export const api = new Client({
  baseUrl: 'https://api.example.com',
  version,
  token: () => SecureStore.getItemAsync('token'),
  cache: AsyncStorage,
});

const first = await api.call(getV1Orders, { query: (q) => q.page({ size: 20 }) });
const second = await api.page(first); // null on the last page

Because each build pins its own version, an app that users have not updated keeps working through later API releases.

Node ​

The same client works in Node 20 and later, such as in a backend-for-frontend or a script. Node is also where you receive webhooks: verify each one against the raw body before trusting it.

ts
import { verifyWebhook } from '@fusion-stack/client';

export async function POST(request: Request) {
  // The raw body: parsing and re-serialising it would break the signature.
  const body = await request.text();
  if (!(await verifyWebhook(body, request.headers.get('Webhook-Signature'), process.env.WEBHOOK_SECRET!))) {
    return new Response('Invalid signature', { status: 400 });
  }

  const event = JSON.parse(body); // { id, type, api_version, data, included }
  // Deliveries are retried with the same id, so skip ids you have already handled.
  return new Response(null, { status: 204 });
}

Webhook payloads arrive in the version the endpoint is pinned to (event.api_version), shaped like that version's API responses.

Released under the MIT License.