---
title: "Discussion on \"Keeping secrets safe with Next.js API routes\" | Hashnode"
description: "Discussion on \"Keeping secrets safe with Next.js API routes\". Have you ever been tempted to hand over your car keys to a stranger? How about your Facebook login details? Your debit card PIN?\nEach of these are ridiculous scenarios - you wouldn't even contemplate them. When your application contacts other service..."
url: "https://hashnode.com/posts/keeping-secrets-safe-with-nextjs-api-routes/5f8157675841661b519d0260"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Keeping secrets safe with Next.js API routes](https://h.kevincunningham.co.uk/keeping-secrets-safe-with-nextjs-api-routes?utm_source=hashnode&utm_medium=feed)

Have you ever been tempted to hand over your car keys to a stranger? How about your Facebook login details? Your debit card PIN? Each of these are ridiculous scenarios - you wouldn't even contemplate them. When your application contacts other service...

Oct 10, 2020 · 3 min read · h.kevincunningham.co.uk  
Updated Oct 25, 2020  
Read the article

-   [#nextjs](https://hashnode.com/tag/nextjs)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#apis](https://hashnode.com/tag/apis)
-   [#2articles1week](https://hashnode.com/tag/2articles1week)

## Written by

[Kevin Cunningham](https://hashnode.com/@dolearning)

@dolearning · Brighton, UK

  
[View profile](https://hashnode.com/@dolearning)

Hey 👋🏻 ! I'm Kevin, a full-stack developer running a digital prototyping company with my business partner called Spin Up . When I'm not consulting and building for clients, I'm writing content here, curating my newsletter or creating videos . Outside of work, I enjoy handing out with my wife and sons - camping, hiking and kayaking or just curling up with a good book.

## Responses 2

Markdown supported · ⌘↵ to post

[Andrew Ross](https://hashnode.com/@AndrewRoss)

[Feb 7, 2021](https://hashnode.com/posts/keeping-secrets-safe-with-nextjs-api-routes/5f8157675841661b519d0260/comment/601f6ed2636acc2a3822f048)

Great article! For that client/server dependent secret you could use a ternary to further refine it.

const isServer = typeof window === “undefined”

fetch(`${isServer ? process.env.BASE_URL : “ “}/api/hello`).then...

[Fábio Rosado](https://hashnode.com/@FabioRosado)

[Oct 10, 2020](https://hashnode.com/posts/keeping-secrets-safe-with-nextjs-api-routes/5f8157675841661b519d0260/comment/5f8177b27762251b305d8ec1)

This is a great write up Kevin! One thing worth to mention is that if you are using fetch/axios and have to pass an API Token to your header, even if you use a variable from `.env` it will be visible on the network tab of your browser.

I know that this might be a bit out of the scope of this article, but I remember when I learned about keeping secrets safe, I thought I could just use `.env` to hide my API keys until someone told me that you could see them in the network tab haha

So yeah, in this case, the best way to do it is by using a serverless function. Like you said on the article if you use Netlify or Vercel both allow you to use serverless functions and you could fetch your super-secret endpoint with your API tokens there

[Kevin Cunningham](https://hashnode.com/@dolearning)

[Oct 10, 2020](https://hashnode.com/posts/keeping-secrets-safe-with-nextjs-api-routes/5f8157675841661b519d0260/comment/5f8177b27762251b305d8ec1#reply-5f81e0ff5841661b519d07e8)

Totally, Fabio, thanks! Anything that is available to your front end code is available to the client and as you say, the easiest way to find this is through looking at the network tab.

Using authentication and token bearing, like JWTs, are ways to make this harder. All of these security measures are a trade off between complexity, convenience and secrecy.

Thanks for the comment :)
