How to make custom api in NuxtJS 3

Create a custom api in NuxtJS 3.
5 Replies
kapa.ai
kapa.ai3w ago
Hi @Adnan Erlansyah. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To create a custom API in NuxtJS 3, you can use the server directory to define API routes. Nuxt 3 uses Nitro, which allows you to create serverless API endpoints directly in your project. Here’s how you can do it: 1. Create an API Route: - Inside your Nuxt 3 project, create a file in the server/api/ directory. For example, to create an endpoint at /api/hello, create server/api/hello.ts. 2. Define the Handler: - Use defineEventHandler to handle requests. Here’s a simple example:
// server/api/hello.ts
export default defineEventHandler(() => {
return { message: 'Hello from Nuxt 3 API!' }
})
// server/api/hello.ts
export default defineEventHandler(() => {
return { message: 'Hello from Nuxt 3 API!' }
})
3. Dynamic Routes and HTTP Methods: - You can use dynamic parameters by naming files with brackets, e.g., server/api/hello/[name].ts. - To handle different HTTP methods, use file suffixes like .get.ts, .post.ts, etc. Example for a dynamic route:
// server/api/hello/[name].ts
export default defineEventHandler((event) => {
const name = event.context.params.name
return { message: `Hello, ${name}!` }
})
// server/api/hello/[name].ts
export default defineEventHandler((event) => {
const name = event.context.params.name
return { message: `Hello, ${name}!` }
})
Example for method-specific handlers:
// server/api/test.get.ts
export default defineEventHandler(() => 'Test GET handler')

// server/api/test.post.ts
export default defineEventHandler(() => 'Test POST handler')
// server/api/test.get.ts
export default defineEventHandler(() => 'Test GET handler')

// server/api/test.post.ts
export default defineEventHandler(() => 'Test POST handler')
4. Accessing Your API: - You can call these endpoints from your frontend using useFetch, $fetch, or any HTTP client. Example usage in a component:
<script setup lang="ts">
const { data } = await useFetch('/api/hello')
</script>
<script setup lang="ts">
const { data } = await useFetch('/api/hello')
</script>
For more advanced patterns, such as catch-all routes or custom serialization, see the Nuxt 3 server directory documentation. References:
- Nuxt 3 Server Directory & API Recipes ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Adnan Erlansyah
Adnan ErlansyahOP3w ago
@kapa.ai I mean by using axios and create httpClient to custom it.
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To create a custom API client in Nuxt 3 using Axios, you should set up a Nuxt plugin that creates and configures your Axios instance, then provide it to your app for use in components and composables. However, the official Nuxt 3 documentation and recipes recommend using $fetch (which is based on ofetch) for HTTP requests, as it integrates better with Nuxt's SSR and Nitro server engine. The knowledge sources above provide detailed examples for customizing $fetch and creating a custom fetch composable, but do not provide explicit instructions for using Axios in Nuxt 3 as a custom HTTP client. Here’s what is covered in the knowledge sources: - Custom $fetch with Nuxt Plugin:
You can create a custom $fetch instance in a Nuxt plugin, configure it (e.g., set base URL, add authentication headers, handle errors), and expose it via useNuxtApp().$api. This is the recommended approach in Nuxt 3 for custom HTTP clients, as shown in the Custom useFetch recipe:
// plugins/api.ts
export default defineNuxtPlugin((nuxtApp) => {
const api = $fetch.create({
baseURL: '<https://api.nuxt.com',>
// ...custom config
})
return {
provide: { api }
}
})

// plugins/api.ts
export default defineNuxtPlugin((nuxtApp) => {
const api = $fetch.create({
baseURL: '<https://api.nuxt.com',>
// ...custom config
})
return {
provide: { api }
}
})

Then, use it in your components or composables:
const { $api } = useNuxtApp()
const { data } = await useAsyncData('modules', () => $api('/modules'))

const { $api } = useNuxtApp()
const { data } = await useAsyncData('modules', () => $api('/modules'))

- Why not Axios?
The knowledge sources do not mention using Axios directly in Nuxt 3 plugins or composables. The focus is on $fetch/ofetch for better SSR support and integration with Nuxt's features. If you still want to use Axios, you would follow a similar plugin pattern, but this is not covered in the provided sources. Summary:
The knowledge sources do not explicitly cover creating a custom API client with Axios in Nuxt 3. They recommend using $fetch and provide recipes for customizing it via plugins and composables for your API needs. If you need to use Axios specifically, you would need to adapt the plugin pattern, but this is outside the scope of the official documentation and examples provided above. For more details and examples on customizing $fetch, see the Custom useFetch in Nuxt documentation and related recipes. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?