How to Hide an API Key in Client-Side Javascript

后端 未结 3 1622
有刺的猬
有刺的猬 2020-12-03 08:36

Right now I am writing a client-side javascript app that makes a request to the USPS Price Calculator API. In order to make this request, I need to provide my API User ID i

3条回答
  •  旧巷少年郎
    2020-12-03 09:06

    Even with your PHP solution you can't hide your userId. It can be easily printed in browser console by accessing consle.log(userId);. As far as I know anything that is available to client-side is vulnerable and can easily be decoded. Even if you obfuscate your api key it can still be decoded from clientside.

    The right thing to do is to create a PHP wrapper around the API calls that require keys, and then call that wrapper from Javascript.

提交回复
热议问题