I've hide my API key inside .env file in my React app. And I used it through process.env. But When I go to network tab in developers tool of google chrome and check requests there I can see my API key present in the request URL. Therefore my API key is not secured. Anyone will able to get my API key. How can I hide my API from that place as well?
Asked
Active
Viewed 3,356 times
2
-
3If it's making a call client-side, there is literally no way you can hide it. You can use a serverless function to obfuscate if you'd like. Anything server-side will hide it, and just pass the results. – Joel Hager Jun 09 '21 at 04:41
3 Answers
6
There is no way to hide the key on the client-side.
My suggestions:
- Do this call from your back-end, and expose it to your front-end
- Add API HTTP referrer restrictions instead. Only requests from your domain make the call in (1)

wdetac
- 2,712
- 3
- 20
- 42
2
If you want to keep your API key private, don't use it in front end. Just keep it in back end and, first send request to backend and, then from back end, send request to that API server

Pranavan
- 1,287
- 1
- 6
- 18
-
But can't anyone send a request to my backend? How to authenticate if thats actually me sending the request? – Shourya Shikhar Jun 23 '22 at 12:42
-
@ShouryaShikhar Then you need to implement authentication in your server side. – Pranavan Jun 23 '22 at 17:50
-
I'm new to this. How do I carry out the authentication? Can you explain me and if possible with an example? – Shourya Shikhar Jun 24 '22 at 07:37
1
Unsure if this is a service you're building or one your consuming. If you are in control of the service then you can change from using a query param through to an authorization header. When the service is live as long as you are using SSL then the header would be encrypted and the key would not be exposed to those snooping.

Jwilz
- 54
- 4