Frontstack’s API Client (also referred to as “Client SDK” or “SDK”) is a type-safe, auto-generated JavaScript library that allows you to interact with your API.
It is generated from your API schema (blocks, listings, pages, etc.) using the Frontstack CLI.
You don’t have to use the SDK to use Frontstack, but you will miss out on some really nice features, such as:
Types for all API responses and payloads
Autocompletion and type checking for queries and parameters
Dynamic resolving of API endpoints
Authentication and header management
Convenience methods for tracking API usage and monitoring performance
Token management for releases
1
API Schema
Frontstack generates a schema for your API, based on the blocks and pages that you have defined.
The cool part - this schema is a fully compliant OpenAPI 3.1.0 specification, so you can use it with any OpenAPI tooling - for example import it into Postman or generate server stubs in other languages.
2
Translate into TypeScript definitions
The schema is then transformed into a set of TypeScript interfaces and types that define your API.
.frontstack/generated-types.d.ts
Copy
// Extract from the fileexport type CategoryDetail { id: string name: string description: string products: Product[]}export type Responses = { CategoryDetail: CategoryDetail}
3
Generate client module
A client module is generated that provides convenient, typesafe and annotated methods for interacting with the API.
.frontstack/generated-client.js
Copy
// Extract from the fileimport type { Blocks, RequestOptions, Responses } from './generated-types';client = { block: <ApiName extends keyof Blocks>( name: ApiName, key: string, config?: RequestOptions ) => Promise<Responses[ApiName]>};export default client;
4
Use the client
You can now use the client library in your frontend project
index.ts
Copy
import { client } from './.frontstack/generated-client'const data = await client.block('CategoryDetail', 'running-shoes')
The final user code in your project will look something like this:
Now that you have connected your project, you can generate the client.
Copy
frontstack generate
Copy
> Javascript client generated for ...
This command will generate four files into a .frontstack directory in your project root:
generated-client.js - The client that you can import into your project
fetch-api.d.t - Type definitions for the client
generated-types.d.ts - Type definitions for the schema
query-types.ts - Type definitions for queries
These files should be committed to your repository, as they represent the API contract between your project and the Frontstack API. In other words - any changes to these files indicate that the API has changed.
In case you get an error, you can try to delete the .frontstack-local directory and run the login and project commands again.
If you are using the frontstack command, make sure that it is installed globally and updated to the latest version (run pnpm add -g @frontstackdev/cli).
Check that you are using the latest version using frontstack --version or npx @frontstackdev/cli --version.