Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active July 19, 2025 18:56
Show Gist options
  • Save Kcko/bf4c68a10b7a56e654192cd1d5cc0c26 to your computer and use it in GitHub Desktop.
Save Kcko/bf4c68a10b7a56e654192cd1d5cc0c26 to your computer and use it in GitHub Desktop.
// plugins/api.ts
export default defineNuxtPlugin((nuxtApp) => {
const api = $fetch.create({
baseURL: 'https://api.example.com',
onRequest({ options }) {
options.headers.set('Authorization', 'Bearer your-token');
},
});
return {
provide: {
api,
},
};
});
Using it in a co
<script setup lang="ts">
const { $api } = useNuxtApp();
const { data } = await $api('/endpoint');
</script>
<template>
<div>{{ data }}</div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment