Skip to main content
Version: v10.0.0

Laravel

Scramble reads your Laravel routes, form requests and API resources and builds an OpenAPI document without annotations. The HyperFetch CLI reads that document and writes a typed TypeScript SDK, so your frontend calls every route with your validation rules and resources as types.


Purpose
  1. No annotations: Scramble infers the document from the code you already have.
  2. Validation rules as types: Form request rules become the payload type of each request.
  3. One command to resync: Change a route, regenerate, and the compiler shows every call to fix.

How it fits together​

StepWhereWhat happens
1LaravelScramble serves /docs/api.json built from your routes
2Terminal@hyper-fetch/cli generate reads it and writes src/api/laravel.sdk.ts
3FrontendcreateSdk(client) gives you typed requests for every route

1. Serve the OpenAPI document​

Install Scramble:

composer require dedoc/scramble

It documents the routes under api/ with no configuration:

routes/api.php
Route::get('/users/{user}', fn (User $user) => new UserResource($user));

The document is on http://localhost:8000/docs/api.json. To write it to a file, run php artisan scramble:export.

The /api prefix

Scramble lists the paths without the api/ prefix and puts it in the server URL instead. Give the HyperFetch client the same base URL, ending with /api.


2. Generate the SDK​

Run the CLI in your frontend project:

npx @hyper-fetch/cli@latest generate --template openapi --url http://localhost:8000/docs/api.json --fileName laravel.sdk.ts

It writes laravel.sdk.ts to src/api. On the first run it also creates api.json with that location. Every path becomes a key and every method a $-prefixed key on it, so GET /users/{user} is sdk.users.$user.$get.


3. Call your API​

import { createClient } from "@hyper-fetch/core";

import { createSdk } from "./api/laravel.sdk";

export const client = createClient({ url: "http://localhost:8000/api" });
export const sdk = createSdk(client);


import { sdk } from "./client";

// Typed from UserResource
const { data, error } = await sdk.users.$user.$get
.setParams({ user: 42 })
.send();

The same requests work with useFetch and useSubmit from @hyper-fetch/react, with caching, retries and deduplication included.


Keep the SDK in sync​

Install the CLI in the frontend project and add a script, so regenerating is one command locally and in CI:

package.json
{
"scripts": {
"api:generate": "hyper-fetch generate -t openapi -u http://localhost:8000/docs/api.json -f laravel.sdk.ts -o"
}
}

-o overwrites the old file without asking. To generate without a running server, export the document with php artisan scramble:export and pass the file to --url instead.

When it's helpful
  • Your backend is Laravel and your frontend is a TypeScript app.
  • You want changes to form requests and resources to show up as TypeScript errors.
  • You want caching, retries and React hooks on top of the generated client.
OpenAPI TypeScript client
Everything the generated SDK can do, from React hooks to regenerating it in CI.
Summary
  • Scramble builds an OpenAPI document from your Laravel routes.
  • @hyper-fetch/cli generate turns it into a typed SDK.
  • createSdk(client) gives you typed requests for send(), useFetch and useSubmit.