From my understanding you want to protect your API key, but at the same time, the call to the API will be made from the client by some JS code you send him.
So how can you achieve this?
1. Scramble your js file - you can scramble your js file (jscrambler, obfuscator) so your code is irrecognizable. This however does not truly protect your js, if, by luck a client finds the scrambled variable corresponding to the API key, then they have access to it (though it's unlikely that happens).
2. Identify your client - The other option to do this is to identify your client and only allow identified clients to access your API (extra security on top of the API key). To achieve this you can:
- Store your clients IP address - ask/collect your client's IP address on registry and store it in your DB. Then on the request, the IP address is collected and used to check if the client can access your API. However this process can fail if your clients don't have a static IP address.
- Create a client fingerprint - Fingerprint2 with several elements of the client's machine (OS, screen resolution, IP address, browser, etc) you can create a "fingerprint" for each of your clients. It's not 100% reliable (pretty close though) and you would have to store it on the registry as well.
- Create an API key for each client - You can also create an API key for each client and store it in the DB. This API key could be generated with the information of either of the 2 methods above (let's say for example you encrypt the IP address and that's your API key). Then, when JS code executed the call to your API, the IP address of the request would be encrypted and checked if it matched the API key.
All these methods are not 100% fail-proof, and you can find false-positives and false-negatives. But if you're supplying the client with the JS code, you're already limited in terms of security.
I hope this gives you some ideas of what you can do to protect your API.
Feel free to ask more if you have any other question or want more details about my suggestions!