> 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`). > - 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: Methods for HTTP Functions ## Article: Methods for HTTP Functions ## Article Link: https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/methods-for-http-functions.md ## Article Content: # Methods for HTTP Functions Each HTTP function contains a method definition that is used to call the corresponding [custom site API](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/expose-a-site-api-with-http-functions.md). The method is defined in the function declaration: ```js export function _(request) { } ``` HTTP functions support the following method declarations: - [`get`](#get) - [`post`](#post) - [`put`](#put) - [`delete`](#delete) - [`use`](#use) ## Methods ### `get` Calls your custom site API using the HTTP `GET` method. Usually, `GET` methods are used only to retrieve a resource. If the resource is found, your function should respond with a [200 (OK) status code](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/ok.md) and the requested resource. Learn more about [`get` HTTP functions](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/get.md). #### Example: Create a GET HTTP function that queries a collection to find items based on the path of the request ```js import { ok, notFound, serverError } from "wix-http-functions"; import wixData from "wix-data"; export function get_myFunction(request) { let options = { headers: { "Content-Type": "application/json", }, }; // Query a collection to find matching items. return ( wixData .query("myUserCollection") .eq("firstName", request.path[0]) .eq("lastName", request.path[1]) .find() .then((results) => { // Matching items are found. if (results.items.length > 0) { options.body = { items: results.items, }; return ok(options); } // No matching items are found. options.body = { error: `'${request.path[0]} ${request.path[1]}' was not found`, }; return notFound(options); }) // Something went wrong. .catch((error) => { options.body = { error: error, }; return serverError(options); }) ); } ``` ### `post` Calls your custom site API using the HTTP `POST` method. Usually, `POST` methods are used to create a new resource. If the resource is successfully created, your function should respond with a [201 (Created) status code](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/created.md) and a reference to the created resource. Learn more about [`post` HTTP functions](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/post.md). #### Example: Create a POST HTTP function that inserts an item from the request's body into a collection ```js import { created, serverError } from "wix-http-functions"; import wixData from "wix-data"; export function post_myFunction(request) { let options = { headers: { "Content-Type": "application/json", }, }; // Get the new item from the request body. return ( request.body .text() .then((body) => { // Insert the item in a collection. return wixData.insert("myUserCollection", JSON.parse(body)); }) .then((results) => { options.body = { inserted: results, }; return created(options); }) // Something went wrong. .catch((error) => { options.body = { error: error, }; return serverError(options); }) ); } ``` ### `put` Calls your custom site API using the HTTP `PUT` method. Usually, `PUT` methods are used to update a resource. If the resource is successfully updated, your function should respond with a [200 (OK) status code](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/ok.md). If the resource didn't exist so it was created, your function should respond with a [201 (Created) status code](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/created.md). Learn more about [`put` HTTP functions](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/put.md). #### Example: Create a PUT HTTP function that updates an item from the request's body in a collection ```js import { ok, serverError } from "wix-http-functions"; import wixData from "wix-data"; export function put_myFunction(request) { let options = { headers: { "Content-Type": "application/json", }, }; // Get the item from the request body. return ( request.body .text() .then((body) => { // Update the item in a collection. return wixData.update("myUserCollection", JSON.parse(body)); }) .then((results) => { options.body = { inserted: results, }; return ok(options); }) // Something went wrong. .catch((error) => { options.body = { error: error, }; return serverError(options); }) ); } ``` ### `delete` Calls your custom site API using the HTTP `DELETE` method. Usually, `DELETE` methods are used to delete a resource. If the resource is successfully deleted, your function should respond with a [200 (OK) status code](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/ok.md). Learn more about [`delete` HTTP functions](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/delete.md). #### Example: Create a DELETE HTTP function that deletes an item from a collection based on the path of the request ```js import { ok, serverError } from "wix-http-functions"; import wixData from "wix-data"; export function delete_myFunction(request) { let options = { headers: { "Content-Type": "application/json", }, }; // delete the item from a collection return ( wixData .remove("myUserCollection", request.path[1]) .then((results) => { options.body = { deleted: results, }; return ok(options); }) // something went wrong .catch((error) => { options.body = { error: error, }; return serverError(options); }) ); } ``` ### `use` Calls your custom site API using any HTTP method, unless there is another function with the same name defined for that specific HTTP method. For example, if you create 2 HTTP functions, called `get_myFunction` and `use_myFunction`, `GET` calls to `myFunction` will be handled by `get_myFunction`, but `POST`, `PUT`, and `DELETE` calls to `myFunction` will be handled by `use_myFunction`. Learn more about [`use` HTTP functions](https://dev.wix.com/docs/velo/api-reference/wix-http-functions/use.md). ## See also - [About custom site API](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/expose-a-site-api-with-http-functions.md) - [Write an HTTP function](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/write-an-http-function.md) - [Custom site API calls](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/integrations/exposing-services/site-api-calls.md)