Nuxt API Party

3.4.2 · active · verified Tue Apr 21

Nuxt API Party is a module for Nuxt that streamlines secure and type-safe interaction with multiple API endpoints. It achieves this by generating composables dynamically for each configured API, similar in feel to Nuxt's native `useFetch` and `$fetch`. The module effectively secures API credentials and circumvents common CORS issues by proxying requests through a Nuxt server route. It features robust OpenAPI specification integration for generating fully typed API clients, offering enhanced developer experience and compile-time safety. The current stable version is 3.4.2, with frequent patch and minor releases addressing bugs and introducing new features. Its key differentiators include automated composable generation, strong type-safety via OpenAPI, built-in credential protection, and smart caching.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates configuring two API endpoints (`jsonPlaceholder` and `myCustomApi`) in `nuxt.config.ts` and then using their respective auto-generated composables (`useJsonPlaceholderData`) and `$api` fetchers (`$myCustomApi`) within a Vue component to fetch and display data.

// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt';

export default defineNuxtConfig({
  modules: ['nuxt-api-party'],
  apiParty: {
    endpoints: {
      jsonPlaceholder: {
        url: process.env.JSON_PLACEHOLDER_API_BASE_URL ?? 'https://jsonplaceholder.typicode.com',
        headers: {
          // In a real app, use environment variables for tokens
          Authorization: `Bearer ${process.env.JSON_PLACEHOLDER_API_TOKEN ?? 'your-mock-token'}`
        }
      },
      myCustomApi: {
        url: process.env.MY_CUSTOM_API_BASE_URL ?? 'http://localhost:3001/api',
        headers: {
          'X-Api-Key': process.env.MY_CUSTOM_API_KEY ?? 'another-mock-key'
        }
      }
    }
  }
});

// app.vue (or any Vue component/page)
<script setup lang="ts">
interface Post {
  userId: number;
  id: number;
  title: string;
  body: string;
}

// Use the auto-generated composable for jsonPlaceholder endpoint
const { data: postData, pending, error, refresh } = await useJsonPlaceholderData<Post>('posts/1');

// Use the $api shorthand for another endpoint
const customApiResponse = await $myCustomApi('/data');
</script>

<template>
  <div>
    <h1>Nuxt API Party Example</h1>
    <div v-if="pending">Loading post...</div>
    <div v-else-if="error">Error loading post: {{ error.message }}</div>
    <div v-else-if="postData">
      <h2>Post Title: {{ postData.title }}</h2>
      <pre>{{ JSON.stringify(postData, null, 2) }}</pre>
      <button @click="refresh">Refresh Post</button>
    </div>
    <hr>
    <h2>Custom API Response (from $myCustomApi)</h2>
    <pre>{{ JSON.stringify(customApiResponse, null, 2) }}</pre>
  </div>
</template>

view raw JSON →