Skip to main content
Version: 1.0.0

Generate Front-end Code to Consume Platformatic REST API

By default, a Platformatic app exposes REST API that provide CRUD (Create, Read, Update, Delete) functionality for each entity (see the Introduction to the REST API documentation for more information on the REST API).

Platformatic CLI allows to auto-generate the front-end code to import in your front-end application to consume the Platformatic REST API.

This guide

  • Explains how to create a new Platformatic app.
  • Explains how to configure the new Platformatic app.
  • Explains how to create a new React or Vue.js front-end application.
  • Explains how to generate the front-end TypeScript code to consume the Platformatic app REST API.
  • Provide some React and Vue.js components (either of them written in TypeScript) that read, create, and update an entity.
  • Explains how to import the new component in your front-end application.

Create a new Platformatic app

Run this command in your terminal to start the Platformatic creator wizard:

npm create platformatic@latest

This interactive command-line tool will ask you some questions about how you'd like to set up your new Platformatic project. For this guide, select these options:

- Which kind of project do you want to create?  => DB
- Where would you like to create your project? => quick-start
- Do you want to create default migrations? => Yes
- Do you want to create a plugin? => Yes
- Do you want to use TypeScript? => No
- Do you want to install dependencies? => Yes (this can take a while)
- Do you want to apply the migrations? => Yes
- Do you want to generate types? => Yes
- Do you want to create the github action to deploy this application to Platformatic Cloud dynamic workspace? => No
- Do you want to create the github action to deploy this application to Platformatic Cloud static workspace? => No

Once the wizard is complete, you'll have a Platformatic app project in the folder quick-start, with example migration files and a plugin script.


Make sure you run the npm/yarn/pnpm command install command manually if you don't ask the wizard to do it for you.

Configure the new Platformatic app

documentation to create a new Platformatic app. Every Platformatic app uses the "Movie" demo entity and includes the corresponding table, migrations, and REST API to create, read, update, and delete movies.

Once the new Platformatic app is ready:

  • Set up CORS in platformatic.db.json
"$schema": "",
"server": {
"hostname": "{PLT_SERVER_HOSTNAME}",
"port": "{PORT}",
"logger": {
+ "cors": {
+ "origin": {
+ "regexp": "/*/"
+ }
+ }

You can find more details about the cors configuration here.

  • launch Platformatic through npm start. Then, the Platformatic app should be available at the URL.

Create a new Front-end Application

Refer to the Scaffolding Your First Vite Project documentation to create a new front-end application, and call it "rest-api-frontend".


Please note Vite is suggested only for practical reasons, but the bundler of choice does not make any difference.

If you are using npm 7+ you should run

npm create vite@latest rest-api-frontend -- --template react-ts

and then follow the Vite's instructions

Scaffolding project in /Users/noriste/Sites/temp/platformatic/rest-api-frontend...

Done. Now run:

cd rest-api-frontend
npm install
npm run dev

Once done, the front-end application is available at http://localhost:5174/.

Generate the front-end code to consume the Platformatic app REST API

Now that either the Platformatic app and the front-end app are running, go to the front-end codebase and run the Platformatic CLI

cd rest-api-frontend/src
npx platformatic client --frontend --language ts

Refer to the Platformatic CLI frontend command documentation to know about the available options.

The Platformatic CLI generates

  • api.d.ts: A TypeScript module that includes all the OpenAPI-related types. Here is part of the generated code
interface GetMoviesRequest {
'limit'?: number;
'offset'?: number;
// ... etc.

interface GetMoviesResponseOK {
'id'?: number;
'title': string;

// ... etc.

export interface Api {
setBaseUrl(baseUrl: string): void;
getMovies(req: GetMoviesRequest): Promise<Array<GetMoviesResponseOK>>;
createMovie(req: CreateMovieRequest): Promise<CreateMovieResponseOK>;
// ... etc.
  • api.ts: A TypeScript module that includes a typed function for every single OpenAPI endpoint. Here is part of the generated code
import type { Api } from './api-types'

let baseUrl = ''
export function setBaseUrl(newUrl: string) { baseUrl = newUrl };

export const createMovie: Api['createMovie'] = async (request) => {
const response = await fetch(`${baseUrl}/movies/`, {
body: JSON.stringify(request),
headers: {
'Content-Type': 'application/json'

if (!response.ok) {
throw new Error(await response.text())

return await response.json()

// etc.

You can add a --name option to the command line to provide a custom name for the generated files.

cd rest-api-frontend/src
npx platformatic client --frontend --name foobar --language ts

will generated foobar.ts and foobar-types.d.ts

React and Vue.js components that read, create, and update an entity

You can copy/paste the following React or Vue.js components that import the code the Platformatic CLI generated.

Create a new file src/PlatformaticPlayground.tsx and copy/paste the following code.

import { useEffect, useState } from 'react'

// getMovies, createMovie, and updateMovie are all functions automatically generated by Platformatic
// in the `api.ts` module.
import { getMovies, createMovie, updateMovie, setBaseUrl } from './api'

setBaseUrl('') // configure this according to your needs

export function PlatformaticPlayground() {
const [movies, setMovies] = useState<Awaited<ReturnType<typeof getMovies>>>([])
const [newMovie, setNewMovie] = useState<Awaited<ReturnType<typeof createMovie>>>()

async function onCreateMovie() {
const newMovie = await createMovie({ title: 'Harry Potter' })

async function onUpdateMovie() {
if (!newMovie || ! return

const updatedMovie = await updateMovie({ id:, title: 'The Lord of the Rings' })

useEffect(() => {
async function fetchMovies() {
const movies = await getMovies({})

}, [])

return (

{movies.length === 0 ? (
<div>No movies yet</div>
) : (
{ => (
<li key={}>{movie.title}</li>

<button onClick={onCreateMovie}>Create movie</button>
<button onClick={onUpdateMovie}>Update movie</button>

{newMovie && <div>Title: {newMovie.title}</div>}

Import the new component in your front-end application

You need to import and render the new component in the front-end application.

Change the App.tsx as follows

import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'

+import { PlatformaticPlayground } from './PlatformaticPlayground'

function App() {
const [count, setCount] = useState(0)

return (
+ <PlatformaticPlayground />
<a href="" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
<a href="" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
Edit <code>src/App.tsx</code> and save to test HMR
<p className="read-the-docs">Click on the Vite and React logos to learn more</p>

export default App

Have fun

Art the top of the front-end application the new component requests the movies to the Platformatic app and list them.

Platformatic frontend guide: listing the movies

Click on "Create movie" to create a new movie called "Harry Potter".

Platformatic frontend guide: creating a movie

Click on "Update movie" to rename "Harry Potter" into "Lord of the Rings".

Platformatic frontend guide: editing a movie

Reload the front-end application to see the new "Lord of the Rings" movie listed.

Platformatic frontend guide: listing the movies .