I’m developing a subscription service using React on the client side and Node.js on the server side. However, I’m encountering an authentication error when attempting to interact with the Stripe API. The error message states that I did not provide an API key, though I expected my setup to handle this correctly. Below are the relevant portions of my client. Has anyone faced a similar problem?
Code client in react native:
const subscribe = async () => {
try {
const response = await fetch(
"https://expressjs-production-0dbf.up.railway.app/create-checkout-session",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
const data = await response.json();
if (!response.ok) {
return Alert.alert(
"Error RESPONSE",
data.message ||
"There was a problem creating the payment session",
);
}
// Redirect the user to the Checkout payment session
const { url, session } = data;
Linking.openURL(url);
update(ref(db, `/users/${auth.currentUser.uid}/user`), {
sessionId: session.id,
});
setModal(null);
} catch (error) {
console.log(error);
Alert.alert(
"Error",
"An error occurred while trying to subscribe",
);
}
};
Normally, stripe opens a session so that the user can access the payment, but I’m getting a 400 error, as shown below:
{"error": {"message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. ‘Authorization: Bearer YOUR_SECRET_KEY’). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/."}}
2
Answers
Here's my server side code, which works fine locally but not in production
You only shared your client-side code. The error seems to be thrown by your server-side code on
/create-checkout-session
route. You should check if you’re initialising Stripe server-side SDK correctly.You can refer to: https://docs.stripe.com/api/authentication