1

I'm fetching data from commercejs.com and my localhost shows some error as "Invalid public key given to Commerce.js client". I choose the public api key one tho

This is my commerce.js:

import Commerce from '@chec/commerce.js';

export const commerce = new Commerce(process.env.REACT_APP_CHEC_PUBLIC_KEY, true);

This is my env:

REACT_APP_CHEC_PUBLIC_KEY=pk_test_32015299720e5050ac997c8e08a77c1a0e24bf21215b7
Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
suga sunshie
  • 107
  • 2
  • 7

3 Answers3

0

Put .env file in your project root

https://commercejs.com/docs/community/faq/

0

Your API key is not being provided to Commerce.js when you construct Commerce.

import { Commerce } from '@chec/commerce.js';

const commerce = new Commerce('put_your_api_key_here');

When using an environment variable, be sure that it is being loaded correctly. In this case please ensure that the .env file in your project is in the project root folder (same level as package.json), and that it has the environment variable defined correctly as referenced by your code. e.g.

const commerce = new Commerce(process.env.REACT_APP_CHEC_PUBLIC_KEY);

project

- node_modules
- src
-.env
-.gitignore
- README.md
- package.json
buddemat
  • 4,552
  • 14
  • 29
  • 49
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '22 at 18:35
0

I'm following the same tutorial.

Try two things:

  • Restart your local server. Stop with CTRL+C, then run npm start.
  • Try using your Public Key (not test key).

Anyone else with issues, double check you've spelled everything correctly in commerce.js. I had written REACT_APP_CHECK instead of CHEC.

Dharman
  • 30,962
  • 25
  • 85
  • 135
R221
  • 1