Skip to content
Advertisement

Basic Auth Send API_KEY to php api – REACTJS

I am sending the username and password to the PHP api with the API_KEY in the .env file. How do I get the user name before the semicolon as the password after the comma in Api? I could not run it with the current code I wrote. I’m using basic auth

.env

JavaScript

REACT CODE

JavaScript

PHP

JavaScript

Advertisement

Answer

You can either write:

JavaScript

Or,

JavaScript

Both are same; will send exact same request headers:

headers

To read the API_KEY i.e. username password from env file, you should name it as REACT_APP_API_KEY and then you can use it in your code as process.env.REACT_APP_API_KEY.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement