> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Expose Custom APIs

## Article: Expose Custom APIs

## Article Link: https://dev.wix.com/docs/coding-examples/extend-functionality/integrations/expose-custom-apis.md

## Article Content:

# Expose Custom APIs

Expose a custom API for a car rental management platform. 

<div style="display: flex; flex-direction: row; align-items: center;"> 
<span style="display: flex; flex-direction: row; align-items: center; margin-right: 20px;"> 
<img src="https://wixmp-833713b177cebf373f611808.wixmp.com/images/08d72650aa29b51886430ccb4db77bd8.png" style="width: 15px; height: 15px;"> 
<span style="margin-left:3px; color:rgb(51 56 83); font-size:1rem">Intermediate</span> 
</span> 
<span style="color:rgb(51 56 83); font-size:1rem">Last updated: 19 May 2025</span> 
</div> 
<div style="display: flex; flex-direction: column; padding-top: 20px; padding-bottom: 20px;">
<div style="width: 100%; margin-bottom: 20px;"> 
<img src="https://static.wixstatic.com/media/d2a594_235c8cd83e594dc6ab7bc4387545d6cd~mv2.webp" alt="Expose Custom APIs" style="width: 100%;"> 
</div> 
<div style="display: flex; flex-direction: row; justify-content: space-between; align-items: center; width: 100%;">
<div style="margin: 0px 20px 20px 0px;">
<a href="fallback::https://manage.wix.com/edit-template/from?originTemplateId=2eeee1ae-b051-42e1-ac6d-93c9e458a25b&templateId=5484a669-bdd6-4a99-ac99-44b00b55967f&autoDevMode=true" target="_blank"> <button style="margin-right: 10px;background-color:#106DFF; color:white; padding:10px 20px; border:none; border-radius:21px; cursor:pointer; margin: 0px 20px 20px 0px;" target="_blank">Open Example</button></a> 
<a href="fallback::https://www.wix.com/velo-examples/httpfunc-lp-s" target="_blank"><button style="background-color:white; color:#106DFF; padding:10px 20px; border:1px solid #106DFF; border-radius:21px; cursor:pointer; width:125px;" target="_blank">Preview</button></a> 
</div> 
</div> 
</div> 
</div>

## About
---

In this example, we demonstrate how to expose an API endpoint from a site. We create a car rental management platform and define an HTTP function. The car rental management system uses the Wix Data API to retrieve and display car data in a repeater, and allow employees to update a car’s price. The HTTP function creates a custom API to retrieve car data, which can be called by external applications.

## APIs 
---
<ul> 
<li><a href="portalId::f356b938-ca2c-46f4-a390-e7ed146e00e0resourceId::4226f67a-73d7-47fd-acd0-e8e474f9f390slug::button/introduction*fallback::https://dev.wix.com/docs/velo/api-reference/$w/button/introduction".md target="_blank">$w.Button API</a></li> 
<li><a href="portalId::f356b938-ca2c-46f4-a390-e7ed146e00e0resourceId::4226f67a-73d7-47fd-acd0-e8e474f9f390slug::repeater/introduction*fallback::https://dev.wix.com/docs/velo/api-reference/$w/repeater/introduction".md target="_blank">$w.Repeater API</a></li> 
<li><a href="portalId::f356b938-ca2c-46f4-a390-e7ed146e00e0resourceId::4226f67a-73d7-47fd-acd0-e8e474f9f390slug::image/introduction*fallback::https://dev.wix.com/docs/velo/api-reference/$w/image/introduction".md target="_blank">$w.Image API</a></li> 
<li><a href="portalId::f356b938-ca2c-46f4-a390-e7ed146e00e0resourceId::4226f67a-73d7-47fd-acd0-e8e474f9f390slug::text/introduction*fallback::https://dev.wix.com/docs/velo/api-reference/$w/text/introduction".md target="_blank">$w.Text API</a></li> 
<li><a href="portalId::f356b938-ca2c-46f4-a390-e7ed146e00e0resourceId::385189be-29a7-4d2c-a8df-f6ba169faae3slug::introduction*fallback::https://dev.wix.com/docs/velo/api-reference/wix-http-functions/introduction".md target="_blank">wix-http-functions</a></li> 
<li><a href="fallback::https://dev.wix.com/docs/sdk/backend-modules/data/introduction".md target="_blank">Data </a></li> 
</ul> 

## How we built it 
---
We added the following to our site:

#### Page Elements

* A repeater to display the car data in the Cars collection. 
* A dropdown element to select a car model. 
* A text input element to enter a price of the selected car model. 
* A button to update the car model’s price. 

#### Collections

**Cars:** A collection containing data about the car models. 
**Cars Location:** A reference collection listing the available locations for the car models.  

#### Frontend Code 

##### Home

The code in this file manages the retrieval, display, and updating of car data for the rental management platform.

In this file, we:
* Declare variables to hold the car data, selected car model, and price input value.
* Retrieve car data from the Cars collection and populate the dropdown options and repeater. 
* Implement event handlers for selecting a car model and inputting a price. 
* Enable and disable the state of the update button based on valid user input. 
* Define an object to hold the car item details for updating the collection. 
* Implement an event handler to update the car item in the collection when the update button is clicked. 
* Refresh the repeater after an update to display the most current data.  


#### Backend Code

##### http-functions.js

The code in this file defines an HTTP function for retrieving car data from the Cars collection, allowing external applications to access the car data in JSON format by making a GET request to the following URL: 

`“https://velo-examples.wixstudio.com/data-example-sdk/_functions-dev/cars”`

We defined the following function:
* `get_cars(request)`: This function queries the Cars collection and returns the data, including the referenced car location details, as a JSON response. It also returns the appropriate HTTP status code based on the success or failure of the request. 


## Code Snippets
---
<details open> 
<summary> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 18" width="22" height="22">
    <path d="M16.003 15.18c0 .57-.11 1.01-.31 1.32.2.31.31.75.31 1.32 0 .94.33 1.22.53 1.24.27.02.481.25.471.52-.01.27-.22.48-.49.48-.521 0-1.512-.47-1.512-2.24 0-.51-.1-.74-.16-.81h-.06c-.06 0-.11 0-.16-.01a.505.505 0 0 1-.561-.29.522.522 0 0 1-.04-.21c0-.08 0-.17.04-.25.1-.22.35-.32.58-.27h.2c.06-.06.17-.29.17-.81 0-1.77.992-2.24 1.513-2.24.28 0 .5.22.5.5s-.22.5-.5.5c-.18.02-.511.3-.511 1.24l-.01.01Zm5.967 1.1c-.09-.22-.33-.33-.57-.28h-.211c-.06-.06-.17-.29-.17-.81 0-1.77-.991-2.24-1.512-2.24-.27 0-.48.21-.49.48-.01.27.2.5.47.52.2.02.53.3.53 1.24 0 .57.11 1.01.311 1.32-.2.31-.31.75-.31 1.32 0 .94-.33 1.22-.511 1.24-.28 0-.5.22-.5.5s.22.5.5.5c.52 0 1.512-.47 1.512-2.24 0-.51.1-.74.16-.81h.06c.06 0 .11 0 .15-.01.23.06.47-.05.57-.27.041-.08.041-.17.041-.25 0-.07 0-.14-.04-.21h.01ZM17.014 4H6.001C5.451 4 5 4.45 5 5v14c0 .55.45 1 1.001 1h7.008v-1H6.001V5h11.013v6h1.001V5c0-.55-.45-1-1-1Zm-9.01 7h7.008v-1H8.004v1Zm7.008-4H8.004v1h7.008V7Z"></path>
  </svg> http-functions.js </summary> 

```javascript
import { ok, notFound, serverError } from 'wix-http-functions';
import {items} from '@wix/data';


// For information on consuming data from this site's collections,
// read here: https://dev.wix.com/docs/velo/velo-only-apis/wix-http-functions/wix-http-function-request.md

export async function get_cars(request) {
    let response = {
        "headers": {
            "Content-Type": "application/json"
        }
    };
    return await items.query("cars")
        .include("location")
        .find()
        .then((results) => {
            if (results.items.length > 0) {
                response.body = {
                    "items": results.items
                };

                return ok(response);
            }
            // no matching items found
            response.body = {
                "error": `no items found`
            };

            return notFound(response);
        })
        .catch((error) => {
            response.body = {
                "error": error
            };
            return serverError(response);
        });
}

```
</details>

<details open> 
<summary> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 18" width="22" height="22">
    <path d="M16.003 15.18c0 .57-.11 1.01-.31 1.32.2.31.31.75.31 1.32 0 .94.33 1.22.53 1.24.27.02.481.25.471.52-.01.27-.22.48-.49.48-.521 0-1.512-.47-1.512-2.24 0-.51-.1-.74-.16-.81h-.06c-.06 0-.11 0-.16-.01a.505.505 0 0 1-.561-.29.522.522 0 0 1-.04-.21c0-.08 0-.17.04-.25.1-.22.35-.32.58-.27h.2c.06-.06.17-.29.17-.81 0-1.77.992-2.24 1.513-2.24.28 0 .5.22.5.5s-.22.5-.5.5c-.18.02-.511.3-.511 1.24l-.01.01Zm5.967 1.1c-.09-.22-.33-.33-.57-.28h-.211c-.06-.06-.17-.29-.17-.81 0-1.77-.991-2.24-1.512-2.24-.27 0-.48.21-.49.48-.01.27.2.5.47.52.2.02.53.3.53 1.24 0 .57.11 1.01.311 1.32-.2.31-.31.75-.31 1.32 0 .94-.33 1.22-.511 1.24-.28 0-.5.22-.5.5s.22.5.5.5c.52 0 1.512-.47 1.512-2.24 0-.51.1-.74.16-.81h.06c.06 0 .11 0 .15-.01.23.06.47-.05.57-.27.041-.08.041-.17.041-.25 0-.07 0-.14-.04-.21h.01ZM17.014 4H6.001C5.451 4 5 4.45 5 5v14c0 .55.45 1 1.001 1h7.008v-1H6.001V5h11.013v6h1.001V5c0-.55-.45-1-1-1Zm-9.01 7h7.008v-1H8.004v1Zm7.008-4H8.004v1h7.008V7Z"></path>
  </svg> Home </summary> 

```javascript
import {items} from "@wix/data";

let carsData;
let selectedVehicleModel;
let priceInput;

$w.onReady(function () {
    importDataFromCollection();
    initCollectionUpdate();
});

function importDataFromCollection() {
    items
        .query("cars")
        .include("location")
        .find()
        .then((results) => {
            carsData = results.items;
            initDropdownOptions();
            populateCarDataInRepeater(carsData);
        })
        .catch((err) => {
            console.error(err);
        });
}

function populateCarDataInRepeater(cars) {
    $w("#carsRepeater").data = cars;

    $w("#carsRepeater").onItemReady(($item, itemData, index) => {
        $item("#modelNameText").text = itemData.model;
        $item("#modelPriceText").text = `$${itemData.price}`;

        for (let i = 0; i < itemData.location.length; i++) {
            if (itemData.location[i]) {
                $item("#locationContainer").children[i].text = itemData.location[i].locationName;
                $item("#locationContainer").children[i].show();
            }
        }
    });
}

function initDropdownOptions() {
    for (let i = 0; i < carsData.length; i++) {
        let modelName = carsData[i].model;

        let opts = $w("#modelNameDropdown").options;
        opts.push({ label: modelName, value: modelName });
        $w("#modelNameDropdown").options = opts;
    }
}

export function initCollectionUpdate() {
    let carItemObjToUpdate;

    $w("#modelNameDropdown").onChange(() => {
        for (let i = 0; i < carsData.length; i++) {
            if (carsData[i].model === $w("#modelNameDropdown").value) {
                carItemObjToUpdate = {
                    _id: carsData[i]._id,
                    model: "",
                    price: "",
                    image: carsData[i].image,
                    gear: carsData[i].gear,
                    numberOfDoors: carsData[i].numberOfDoors,
                    maximumNumberOfPassengers: carsData[i].maximumNumberOfPassengers,
                    trunkCapacity: carsData[i].trunkCapacity,
                    carLocation: carsData[i].carLocation,
                };

                selectedVehicleModel = carsData[i].model;
                setupdateBtnMode(selectedVehicleModel, priceInput);
            }
        }
    });

    $w("#priceInput").onInput(() => {
        priceInput = $w("#priceInput").value.toString();
        setupdateBtnMode(selectedVehicleModel, priceInput);
    });

    $w("#updateBtn").onClick(() => {
        carItemObjToUpdate.model = selectedVehicleModel;
        carItemObjToUpdate.price = priceInput;

        items
            .update("cars", carItemObjToUpdate)
            .then(() => {
                refreshTheRepeater();
            })
            .catch((err) => {
                console.error(err);
            });
    });
}

export function setupdateBtnMode(selectedVehicleModel, priceInput) {
    if (
        selectedVehicleModel === undefined ||
        priceInput === undefined ||
        priceInput <= 0
    ) {
        $w("#updateBtn").disable();
    } else {
        $w("#updateBtn").enable();
    }
}

export function refreshTheRepeater() {
    items.query("cars")
        .find()
        .then((results) => {
            carsData = results.items;

            $w("#carsRepeater").data = carsData;
            $w("#carsRepeater").onItemReady(async ($item, itemData, index) => {
                $item("#modelNameText").text = itemData.model;
                $item("#modelPriceText").text = `$${itemData.price}`;
            });
        })
        .then(() => {
            $w("#priceInput").value = null;
            $w("#modelNameDropdown").selectedIndex = undefined;
            selectedVehicleModel = undefined;
            priceInput = undefined;
            setupdateBtnMode(selectedVehicleModel, priceInput);
        })

        .catch((err) => {
            console.error(err);
        });
}

```
</details>

<!-- Static content -->
## Get Help
--- 
### Join the community 

Join the [Wix Studio][discord-community] community on Discord, where experienced developers and fellow creators come together to share tips, troubleshoot issues, and collaborate.

### Hire a developer 

Building a coded solution on your own can be challenging. Let an experienced Wix developer build it for you, so you can keep working on your site or business.
Visit the [Wix Marketplace][wix-marketplace] to find a trusted developer. 

<!-- Predefined links -->
[discord-community]: https://discord.com/invite/GhdmDN926z 
[wix-marketplace]: https://www.wix.com/marketplace/brief-auto-match/web-developer?entry=velo&origin=examples-page "Wix Marketplace"