Should we be using Backend HTTP functions in WIX Studio

http-functions.js is not working.

Getting a 500 error with a simple function. Like get_test

No logs appear in Site Monitoring.

My backend code is correct and the file is in the right place.

I was trying to use Backend to download a CSV from the data held in the CMS Collection for members who have signed in.

My collections and field keys are correct and work elsewhere on your site.

This does seem to be a platform-level issue, not a code or logic error.

I’m not able to recreate. Can you share the code you’re using and where it’s located?

It was more a general question whether the Backend HTTP Function is available in WIX Studio.

The code in the frontend has been deleted because I needed to go back to the original code I had but here is the Backend code -

import wixData from ‘wix-data’;

export async function get_downloadBookingsCsv(request) {

**const** email = request.query\["email"\];

console.log("Email received:", email);



**if** (!email) {

    console.log("No email provided");

    **return** {

        status: 400,

        body: "Missing email parameter"

    };

}



**const** ownerResults = **await** wixData.query("OwnerInformation")

    .eq("ownerEmail", email)

    .find();

console.log("Owner results:", ownerResults.items);



**if** (ownerResults.items.length === 0) {

    console.log("Owner not found");

    **return** {

        status: 404,

        body: "Owner not found"

    };

}



**const** ownerId = ownerResults.items\[0\].\_id;



**const** villaResults = **await** wixData.query("Villas")

    .eq("ownerEmailRef", ownerId)

    .find();

console.log("Villa results:", villaResults.items);



**if** (villaResults.items.length === 0) {

    console.log("No villas found");

    **return** {

        status: 404,

        body: "No villas found"

    };

}



**const** villaIds = villaResults.items.map(v => v.\_id);

**const** villaMap = {};

villaResults.items.forEach(v => {

    villaMap\[v.\_id\] = {

        villaName: v.villaName,

        community: v.community

    };

});



**const** bookingResults = **await** wixData.query("Bookings")

    .hasSome("villasReference", villaIds)

    .find();

console.log("Booking results:", bookingResults.items);



**if** (bookingResults.items.length === 0) {

    console.log("No bookings found");

    **return** {

        status: 404,

        body: "No bookings found"

    };

}



// Build CSV

**let** csv = "Villa Name,Community,Guest Name,Check-In,Check-Out,Nights,Guests,Adults,Children,Pool Heating,Additional Requirements\\n";

bookingResults.items.forEach(item => {

    **let** villaInfo = villaMap\[item.villasReference\] || {};

    **let** nights = "";

    **if** (item.checkInDate && item.checkOutDate) {

        **const** checkIn = **new** Date(item.checkInDate);

        **const** checkOut = **new** Date(item.checkOutDate);

        **const** diffTime = checkOut - checkIn;

        nights = Math.round(diffTime / (1000 \* 60 \* 60 \* 24));

    }

    csv += \[

        \`"${villaInfo.villaName || ""}"\`,

        \`"${villaInfo.community || ""}"\`,

        \`"${item.guestName || ""}"\`,

        \`"${item.checkInDate ? **new** Date(item.checkInDate).toLocaleDateString() : ""}"\`,

        \`"${item.checkOutDate ? **new** Date(item.checkOutDate).toLocaleDateString() : ""}"\`,

        \`"${nights}"\`,

        \`"${item.numberOfGuests || ""}"\`,

        \`"${item.numberOfAdults || ""}"\`,

        \`"${item.numberOfChildren !== **null** && item.numberOfChildren !== **undefined** ? item.numberOfChildren : ""}"\`,

        \`"${item.poolHeating === **true** ? "Yes" : "No"}"\`,

        \`"${item.additionalRequirements || ""}"\`

    \].join(",") + "\\n";

});



console.log("CSV generated, length:", csv.length);



**return** {

    status: 200,

    headers: {

        "Content-Type": "text/csv",

        "Content-Disposition": "attachment; filename=bookings.csv"

    },

    body: csv

};

}

Also a Test code -

export function get_test(request) {

**return** {

    status: 200,

    body: "Test function is working"

};

}

I think I see what’s tripping it up.

Wix has some specific ways of approaching http-functions. At the moment, you’re have a return - but it needs to use one of the response options, like ok(), notFound(), serverError() - Velo Wix Http Functions Introduction | Velo

From the code you shared - it doesn’t seem you’re doing that

That’s an important question. From my experience, backend HTTP functions in Wix Studio are best used when you want to keep API keys or server-side logic secure.
Using them also helps reduce client-side loading time and improves data security.
However, for simple fetch requests that don’t involve sensitive information, front-end HTTP functions can still work fine.
It really depends on the complexity of your integration and the type of data you’re handling.

Thank you, I have corrected the Backend code as suggested and it now works. Members can now download a CSV file of their data from the CMS Collection.

1 Like

Amazing! Glad we could help :slight_smile: