#Private API to fetch data from server on page load or navigation without exposing api to the client

1 messages · Page 1 of 1 (latest)

cursive cedar
#

Hello,

Is it possible to have a completely private API in Nuxt without exposing public server API?

We have a page called example.vue and we want to load data from the server when the client navigates to the page for the first time. All fetching processes should be on the server side, and the private API should not be visible to the clients.

<!-- pages/example.vue -->
<script setup>
// This should be called on the server side
const { data } = await useAsyncData(
  "theKey",
  () => $fetch("external api"), // This code should not be visible to clients
  {
    server: true, // Set to true but data won't load on navigation
  }
);
</script>

<template>
  <div>
    <!-- Do something with data on the client side -->
  </div>
</template>

we don't want to create server/api/get-data-from-external-api.ts because anyone can hit this endpoint.

Any ideas?

mortal spade
#

In the server endpoint you proposed that "anyone" can hit you want to use an API key that will be used to authenticate with the external backend. This key will then only be visible on your server.