Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) AJAX Concepts AJAX Security Limitations

lesak
lesak
6,260 Points

API Key - how to hide

Hello. I'm wondering if it is a bad idea to expose an api key on the client side and if it is, how to hide it. Could any one shed light on this for me or lead me into the right direction about where to find more information on this? Thank you.

2 Answers

akak
akak
29,445 Points

Hi,

There is no solid way to hide it on frontend. Also have in mind not all keys needs to be hidden. But if you'd like to make sure no one sees it the key should be stored on backend. So for example if you have a REST API you would do request like: myserver/getWeather and the backend would make a request to the weather service using your api key and just patch data back to the frontend.

lesak
lesak
6,260 Points

Hi akak. Thanks for clearing this up for me. I like the prospect of working with APIs so I'm glad that not all keys need to be hidden. I read on a blog that if the key is given and associated with a domain then it's also okay to include the key on frontend since it'll only work within this domain. I'm curious about what happens in the backend now but first I need to get my head around the frontend..