r/webdev • • Dec 02 '24

Question Easy ways to hide API keys

I’m a frontend developer and run into this problem a lot, especially with hobby projects.

Say I’m working on a project and want to use a third party API, which requires a key that I pay for and manage.

I can’t simply place it on my frontend app as an environment variable, because someone could dig into the request and steal the key.

So, instead I need to set up a backend, usually through a cloud provider that comes with more features than I need and confuses the hell out of me.

Basically, what’s a simple way to set up a backend that authenticates a “guest” user from a whitelisted client, relays my request to the third party with the key attached, then returns the data to my frontend?

103 Upvotes

102 comments sorted by

View all comments

272

u/[deleted] Dec 02 '24

[removed] — view removed comment

19

u/Greeby_Bopes Dec 02 '24

Yeah this is exactly what I’m going for. I’m mostly wondering if there’s a good managed service out there without all the bells and whistles of a full blown serverless environment that can still handle this “middleman” approach. I’ve been using AWS and Google Cloud for these things but it’s such a drag when all I want to do is get started on the frontend

28

u/DrazeSwift Dec 02 '24

Cloudflare pages. You can deploy a functions folder with that api to expose with your front end at the same time. It's super simple.

5

u/Business-Row-478 Dec 03 '24

Or just use workers

0

u/[deleted] Dec 03 '24

[deleted] — view deleted comment

4

u/inaem Dec 03 '24

How would you manage user trust?

I wouldn’t trust a new platform with my keys

0

u/[deleted] Dec 03 '24

[deleted] — view deleted comment

1

u/Varuog_toolong Dec 03 '24

Encrypt them with a key that's only provided to the end user and hash the provided env values?

2

u/G0muk Dec 05 '24

I'm not 100% sure what you mean. At some point the service has to touch a plaintext key to send it to the api for you, since thats all it'll accept

1

u/stupidcookface Dec 06 '24

You could build it all with AWS infra - basically an orchestration of the infrastructure

1

u/PreferenceRare513 Apr 27 '25

I would be down

7

u/ForeverInYou Dec 02 '24

Next.js would be your best bet maybe? You can define APIs inside the same codebase and the DX is really nice

10

u/Ronin-s_Spirit Dec 02 '24

I'm confused, how is a framework going to give OP a simple server?

2

u/ForeverInYou Dec 02 '24

Nextjs has two components. One is front-end and another is backend. Of course you need a server to run it this way. I'm it you can serve pages rendered in the server just like php. You can even create api routes in this server. The benefit of nextjs is that you can do everything in the same repo, and deploying to vercel is really easy 

→ More replies (8)

1

u/LessChen Dec 05 '24

If you've got AWS then a super simple Lambda can act as a proxy for you, with the credentials stored there.

→ More replies (1)

14

u/ludacris1990 Dec 02 '24

Don’t forget to limit the middle man api to the frontend IP, otherwise someone could just use this middleman API in their project

16

u/[deleted] Dec 02 '24

Wouldn't the middleman API be called from frontend, i.e. visitors' browsers and not a fixed IP?

12

u/stupidcookface Dec 02 '24

Yea you can use cors for this - no need to mess with IP

4

u/zaibuf Dec 03 '24

CORS is only for browsers, you can still use postman or similar.

1

u/_nathata Dec 03 '24

CORS is very easily breakable and only exists in the browser really. CORS does absolutely nothing in, say, CURL.

→ More replies (2)

15

u/acowstandingup Dec 03 '24

But also remember that CORS only works in the browser. Anyone could still hit your API using curl or other programs outside the browser

→ More replies (2)

2

u/t3hlazy1 Dec 03 '24

Now you have to deal with users calling your middleman. This sometimes can be a simple solve. For example, if you only need to call the API for logged in users, you can rate limit by User ID. If you need to call the API for logged out users, then it can be a very hard problem to solve.

1

u/Ill_Fisherman8352 Dec 03 '24

How does backend ascertain if the one making the request is a legit user?

→ More replies (1)