How to share `response` between routes in Express for PayPal subscription?

I’m working on integrating PayPal subscriptions into my Node.js application using Express and encountered a problem sharing the subscription ID obtained from PayPal’s /create-subscription route with the /execute-subscription route.

The goal is to capture the subscriptionId from the response of the /create-subscription route:

const response = await axios.post("https://api-m.sandbox.paypal.com/v1/billing/subscriptions", subscriptionDetails, {
  headers: {
    Authorization: `Bearer ${access_token}`,
  },
});

And use it in the /execute-subscription route to activate the subscription:

const subscriptionId = response.data.id; 

However, I’m not sure how to properly pass subscriptionId between these two routes. Directly accessing response.data.id in the /execute-subscription route results in an undefined error, likely because response is not in scope.

Code Snippet

paypalRouter.post("/create-subscription", async (req,res) => {
  try {
    const subscriptionDetails = {
      plan_id: process.env.PAYPAL_SANDBOX_BUSSINESS_SUBSCRIPTION_PLAN_ID,
     
      application_context: {
        brand_name: "brand",
        return_url: "http://localhost:3001/paypal/execute-subscription",
        cancel_url: "http://localhost:3001/paypal/cancel-subscription",
      }
    };

    // Generar un token de acceso
    const params = new URLSearchParams();
    params.append("grant_type", "client_credentials");
    const authResponse = await axios.post(
      "https://api-m.sandbox.paypal.com/v1/oauth2/token",
      params,
      {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        auth: {
          username: process.env.PAYPAL_CLIENT_ID,
          password: process.env.PAYPAL_SECRET,
        },
      }
    );

    const access_token = authResponse.data.access_token;

    const response = await axios.post(
      "https://api-m.sandbox.paypal.com/v1/billing/subscriptions",
      subscriptionDetails,
      {
        headers: {
          Authorization: `Bearer ${access_token}`,
        },
      }
    );

    console.log(response.data.id);
    console.error();
    return res.json({ subscriptionId: response.data.id, ...response.data });
  } catch (error) {
    console.log(error);
    return res.status(500).json("Something goes wrong");
  }
});



paypalRouter.get("/execute-subscription", async (req, res) => {
  const { token } = req.query; // El token de la suscripción que PayPal envía de vuelta

  try {
    // Paso 1: Obtener el Token de Acceso
    const params = new URLSearchParams();
    params.append("grant_type", "client_credentials");
    const authResponse = await axios.post("https://api-m.sandbox.paypal.com/v1/oauth2/token", params, {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      auth: {
        username: process.env.PAYPAL_CLIENT_ID,
        password: process.env.PAYPAL_SECRET,
      },
    });

    const access_token = authResponse.data.access_token;

    const subscriptionId = response.data.id; 
    const executeResponse = 
    await axios.post(`https://api-m.sandbox.paypal.com/v1/billing/subscriptions/${subscriptionId}/activate`, {}, {
      headers: {
        Authorization: `Bearer ${access_token}`,
        'Content-Type': 'application/json'
      },
    });

    console.log("Subscription confirmed:", executeResponse.data);
    console.error();
        res.send("Subscription successful!");
  } catch (error) {
    console.error("Error executing subscription:", error.response ? error.response.data : error.message);
    res.status(500).send("An error occurred while executing the subscription.");
  }
});