Integration with Nuxt
We can use nuxt-elysia, a community plugin for Nuxt, to set up Elysia on Nuxt API routes with Eden Treaty.
- Install the plugin with the following command:
bun add elysia@rc @elysia/eden@nextbun add -d nuxt-elysia- Add
nuxt-elysiato your Nuxt config:
export default defineNuxtConfig({ modules: [ 'nuxt-elysia' ], nitro: { preset: 'bun' }})- Create
api.tsin the project root:
import { Elysia } from 'elysia'
export default () => new Elysia() .get('/hello', () => ({ message: 'Hello world!' }))- Use Eden Treaty in your Nuxt app:
<template> <div> <p>{{ data.message }}</p> </div></template><script setup lang="ts">const { $api } = useNuxtApp()
const { data } = await useAsyncData(async () => { const { data, error } = await $api.hello.get()
if (error) throw new Error('Failed to call API')
return data})</script>This will automatically set up Elysia to run on Nuxt API routes.
If your pnpm configuration does not install peer dependencies, add the peers required by Elysia v2 explicitly:
pnpm add typebox exact-mirror openapi-typesPrefix
Section titled “Prefix”By default, Elysia will be mounted on /_api, but we can customize it with the nuxt-elysia config.
export default defineNuxtConfig({ nuxtElysia: { path: '/api' }})This will mount Elysia on /api instead of /_api.
For more configuration options, please refer to nuxt-elysia.