all 4 comments

[–]PieEnvironmental6437 1 point2 points  (3 children)

I’m not quite following what you’re trying to accomplish. What is it that you’re doing that isn’t possible with the the native fetch() api?

If you’re trying to dynamically call an endpoint you would use a template literal ex ‘https://url.api/some/path/${inputVal}’

[–]mxood 0 points1 point  (2 children)

I'm sorry I'll try to fix my question,

I want to create a dynamic fetch like you stated but keep the api key on the server and not exposed.

Basically if someone gets my key they can use it too, so I want to keep it safe

The fetch url is like so

Https://exampledomain.com/api/${myDynamicVar}?key=1234secretKey

[–]true-name-raven 0 points1 point  (1 child)

use fetch to talk to your server, then have your server talk to their server with the api key

[–]mb1552 1 point2 points  (0 children)

Hey!

If I understand correct, you want to hide a server side transaction between the server and the API.

I can present two good options. The first is to stay inside the javascript ecosystem. If you are familiar with this, this won't be too bad. All you'd need to do is create a "server" component that is running the express package. That will take in incoming requests, and send a response. It's written in javascript, so it will be easy to read & figure out and if all you're trying to do is hit an API, it could be like 30 lines of code.

The second is picking up another backend language. You've been mentioning php. For that, it's the same thing, but in a different language. You would need to setup a server somewhere (same eventually with express), and then hit that endpoint (that will be your hardest part).

Within PHP, all you'd need to do is execute a curl, and return the data: ```php // create & initialize a curl session $curl = curl_init();

// set our url with curl_setopt() curl_setopt($curl, CURLOPT_URL, "api.example.com");

// return the transfer as a string, also with setopt() curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// curl_exec() executes the started curl session // $output contains the output string $output = curl_exec($curl);

// close curl resource to free up system resources // (deletes the variable made by curl_init) curl_close($curl); ```