Getting Started
Nuxt API Party is a Nuxt module for talking to APIs you don't control. You configure an endpoint once, and the module generates a pair of type-safe composables for it that behave like Nuxt's own useFetch and $fetch – while the credentials stay on the server and CORS never comes up, because requests travel through a server proxy in your app, unless an endpoint is a client endpoint.
Install Nuxt API Party
Nuxt API Party requires Nuxt 4.4 or later. Install it via the Nuxt CLI:
npx nuxt module add api-partyConfigure Your First Endpoint
Configure an endpoint in your Nuxt configuration. Each endpoint generates two composables for data fetching:
export default defineNuxtConfig({
modules: ['nuxt-api-party'],
apiParty: {
endpoints: {
// Endpoint ID: `jsonPlaceholder`
jsonPlaceholder: {
url: process.env.JSON_PLACEHOLDER_API_BASE_URL!,
// Optional: Sent as a bearer token with every request, from the server
token: process.env.JSON_PLACEHOLDER_API_TOKEN!,
},
},
},
})url is the only required option. See Module Configuration for the rest, including default headers, credential forwarding and OpenAPI schemas.
Dynamic Configuration
For dynamic headers or runtime configuration, use runtime hooks or environment variables.
Use environment variables instead of hardcoding sensitive values:
# `.env`
JSON_PLACEHOLDER_API_BASE_URL=https://jsonplaceholder.typicode.com
JSON_PLACEHOLDER_API_TOKEN=your-secret-tokenMake Your First Request
For the endpoint jsonPlaceholder configured above, Nuxt API Party generates two composables:
$jsonPlaceholder– Direct API calls, similar to$fetchuseJsonPlaceholderData– Reactive data fetching, similar touseFetch
Configure as many endpoints as you need; each gets its own pair, named after its endpoint ID.
Now you can use the generated composables in your components, and their server imports in your Nitro routes.
Reactive Data Fetching
Use the useJsonPlaceholderData composable for reactive data that updates your component:
<script setup lang="ts">
interface Post {
id: number
title: string
body: string
}
// Fetch a single post
const { data: post, refresh, error, status } = await useJsonPlaceholderData<Post>('posts/1')
// Fetch multiple posts with query parameters
const { data: posts } = await useJsonPlaceholderData<Post[]>('posts', {
query: { _limit: 10 },
})
</script>
<template>
<div>
<!-- Single post -->
<article v-if="post">
<h1>{{ post.title }}</h1>
<p>{{ post.body }}</p>
<button @click="refresh()">Refresh</button>
</article>
<!-- Posts list -->
<div v-if="posts">
<h2>Latest Posts</h2>
<article v-for="item in posts" :key="item.id">
<h3>{{ item.title }}</h3>
</article>
</div>
<!-- Loading & Error states -->
<p v-if="status === 'pending'">Loading...</p>
<p v-if="error">
{{ error.statusText }}
</p>
</div>
</template>Direct API Calls
Use the $jsonPlaceholder composable for programmatic requests, form submissions, and one-time actions:
const post = await $jsonPlaceholder('posts', {
method: 'POST',
body: { title: 'Hello', body: 'World', userId: 1 },
})It throws on a failed request rather than exposing an error value, so wrap it where you need to react to failure.
From here, Error Handling shows what a failed request gives you, OpenAPI Integration how to infer request and response types from a schema, and Caching Strategies how to reuse responses from the payload or the browser's cache.