skip to Main Content

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


  1. Chosen as BEST ANSWER

    Here's my server side code, which works fine locally but not in production

    import { Router } from "express";
    import Stripe from "stripe";
    import { config } from "dotenv";
    
    const routes = Router();
    
    config();
    
    const stripe = new Stripe(process.env.STRIPE_SECRET_KEY_LIVE);
    
    routes.post("/", async (req, res) => {
      try {
        const session = await stripe.checkout.sessions.create({
          line_items: [
            {
              // prod : price_1ObRi1Kww5u0tgAGFdOa.....
              // test : price_1OL2uDKww5u0tgAGM....
              price: "price_1ObRi1Kww5u0tgAGFdO.....",
              quantity: 1,
            },
          ],
    
          mode: "subscription",
          success_url:
            req.headers.origin === undefined
              ? "https://verse-app.netlify.app/successed"
              : `${req.headers.origin}/success`,
          cancel_url:
            req.headers.origin === undefined
              ? "https://verse-app.netlify.app/cancelled"
              : `${req.headers.origin}/cancel`,
        });
    
        res.json({ sessionId: session.id, url: session.url, session: session });
      } catch (error) {
        res.status(400).send({ error: { message: error.message } });
      }
    });
    
    export default routes;
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search