app.get("/test", async (req, res) => {
    const apiKey = '6085F0C343CE482C8A7E186125419DD1'; // Replace with your TripAdvisor API key
    const baseUrl = '';

    try {
        const country = 'taichung'; // Replace with the desired country or use req.query to get it from the request parameters
        const apiUrl = `${baseUrl}/hotels?query=${country}`;
        const headers = {
            'Content-Type': 'application/json',
            'x-api-key': apiKey,

        const response = await axios.get(apiUrl, { headers });

        if ( && {
            return res.send("Data printed in the console");
        } else {
            console.error('Error fetching hotel data from TripAdvisor:',;
            return res.send('Error fetching hotel data from TripAdvisor:')
            // req.flash("error", "No hotels found. Please try again!");
            // return res.redirect("/hotels");
    } catch (error) {
        console.error('Error:', error.message);
        return res.status(500).send("Internal Server Error");

I got this error , Error: Request failed with status code 401

I have double-checked my TripAdvisor API key, ensured it’s included in the headers, and verified the API endpoint. Despite these checks, the 401 error persists.

Could someone tell me how to fix this ? Thanks !
The TripAdvisor API key is valid, and I’ve successfully used other APIs in my application.