I’ve started my project with Vite.
When I directly use my api key instead of importing it from .env, it works. But when I use it like this I get the error below on the console.
{status_code: 7, status_message: ‘Invalid API key: You must be granted a valid key.’, success: false}
Here’s my code
const apiKey = import.meta.env.REACT_APP_API_KEY;
const getMovies = () => {
fetch(`https://api.themoviedb.org/3/discover/movie?api_key=${apiKey}`)
.then((res) => res.json())
.then((json) => console.log(json))
.catch((err) => console.log(err));
};
useEffect(() => {
return () => {
getMovies();
};
}, []);
3
Answers
It seems that you are trying to access an environment variable from your .env file using import.meta.env.REACT_APP_API_KEY. However, according to the Vite documentation, only environment variables prefixed with VITE_ are exposed to your Vite-processed code. To access your API key, you should prefix it with VITE_ in your .env file and then access it using import.meta.env.VITE_REACT_APP_API_KEY. For example, in your .env file, you should have something like this:
And then in your code, you can access it like this:
I hope this helps..
the variable names in the .env files should start with REACT_APP.
like : REACT_APP_API_KEY = 1234
and for using this variable in you’re app you shouldn’t import it. instead, you should use this variable like this: process.env.REACT_APP_API_KEY.
example : {process.env.REACT_APP_API_KEY}
The error message you’re receiving, "Invalid API key: You must be granted a valid key," suggests that there might be an issue with how the API key is being read from your environment variables. The code you provided for reading the API key from
.env
looks correct, but there could be some reasons why it’s not working as expected.Here are some troubleshooting steps you can follow:
Check
.env
File: First, make sure that your.env
file is correctly configured and contains theREACT_APP_API_KEY
variable with the correct API key value.Restart the Development Server: After making changes to your
.env
file, you should restart your development server to ensure that the changes are picked up.Ensure .env Variables Start with
REACT_APP_
: Environment variables in React applications must start withREACT_APP_
. Make sure your.env
variable is namedREACT_APP_API_KEY
.Check for Typos or Extra Whitespaces: Ensure that there are no typos or extra whitespaces in your API key or variable names in the
.env
file.Verify that .env is in the Root Directory: The
.env
file should be located in the root directory of your React project.Security: Double-check that your API key is correct and hasn’t changed. Sometimes, API providers may regenerate keys, which would invalidate the old one.
React Scripts Version: Ensure you are using a version of
react-scripts
that supports environment variables in.env
files. Newer versions ofcreate-react-app
andreact-scripts
provide better support for environment variables.Bundler Caching: Sometimes, the bundler might cache environment variables. Try clearing your project’s build cache by running:
Browser Cache: Clear your browser’s cache and try reloading the application.
Testing Locally: Try printing the
apiKey
variable to the console to ensure that it’s being read correctly from the environment variables. For example:Verify that it displays the correct API key in the console.
If you’ve checked all of the above and are still experiencing issues, it’s possible that there’s an issue specific to your development environment or setup. In such cases, consider seeking help from a colleague or developer familiar with your development environment to assist in debugging the problem.