Guides

Connect your API

Replace the demo data with your backend without touching pages or components.

composables/useApi.ts

Every screen reads its data through a composable. To use real data you change that composable, or the provider it receives. Pages and components stay as they are.

Piece
File
Purpose
useApi()composables/useApi.ts$fetch with your base URL, the session token and sign-out on a 401.
toFormSubmitError()composables/useApi.tsTurns a 422 from your API into the error forms understand.
Modelstypes/models/ and the types in mocks/The data contract the components expect.

1. Set the URL

env/.env
NUXT_PUBLIC_API_BASE=https://api.example.com

useApi() uses it as baseURL, so paths are relative: api('/customers').

2. Send the token

useApi() adds Authorization: Bearer <token> when the auth-token cookie exists. Save the token when the user signs in, as shown in Authentication. If your API answers 401, useApi() deletes the token and sends the user to /login.

3. Write a provider

Every app and every module receives a provider: an object with the operations it needs, typed in types/models/<area>.ts. The demo passes a mock provider (createCustomersMockProvider() in mocks/customers.mock.ts); write one that calls your API:

providers/customers.ts
import type { CustomersProvider } from '~/types/models/customers'

export function createCustomersApiProvider(): CustomersProvider {
  const api = useApi()

  return {
    list: () => api('/customers'),
    save: (customer) => api(`/customers/${customer.id}`, { method: 'PUT', body: customer }),
    remove: (ids) => api('/customers', { method: 'DELETE', body: { ids } }),
  }
}

Then make it the default in the composable, so every page and component that calls it gets your data:

composables/useCustomers.ts
const provider = options.provider ?? createCustomersApiProvider()

You can also pass it in a call, as in useEmail({ provider: createEmailApiProvider() }). The first call of a module loads the data, so pass it there or make it the default.

The apps load through useAsyncData. The modules load once per visit into shared state and the server waits for them before rendering. Detail pages wait for ready before they look the record up, only while loaded is still false (on the server or a first visit). Both apply changes optimistically: the interface updates at once and the provider call runs behind it. If a module call fails, a toast says so and the module loads its data from the provider again (refresh() from useProviderState()), so the screen drops the change the backend rejected; a failed load leaves its message in error.

Area
Provider
Operations
EmailEmailProviderlist, update, send
ChatChatProviderload, send, updateThread, updateMessage
CalendarCalendarProviderlist, save, remove
KanbanKanbanProviderload, saveCard, removeCard, moveCard, saveColumn, removeColumn
TasksTodoProviderlist, save, remove
FilesFilesProviderload, saveFile, removeFile, saveFolder, removeFolder
OrdersOrdersProviderlist, save
ProductsProductsProviderlist, save, remove
CustomersCustomersProviderlist, save, remove
InvoicesInvoicesProviderlist, save, remove
LearningLearningProviderload, saveCourse, removeCourse, saveStudent, removeStudents, saveInstructor, removeInstructor

The account area (useAccount()) and the help center (useHelpCenter()) still read their mocks directly: replace the mock inside those composables.

4. Validation errors

If your API answers 422 like this:

json
{ "message": "Check the highlighted fields", "errors": { "email": ["This email already exists"] } }

convert it inside the form handler and useFormSubmit() shows each message next to its field:

ts
const { submit } = useFormSubmit(values, async (payload) => {
  try {
    await api('/customers', { method: 'POST', body: payload })
  } catch (error) {
    throw toFormSubmitError(error)
  }
})

5. Actions the demo fakes

Buttons that need a backend in the demo call useToast().show(t('apps.common.demoAction')). Search for demoAction to find every place where you need to wire a real call.

Keep labels out of your data

Return ids, not labels: segment: 'vip', channel: 'web', day: 'mon'. The pages translate them. If your API returns display text, map it to the ids in the composable.

© 2026 melsami UI.
Developer documentation for melsami UI.