wix-http-functions

wix-http-functions

HTTP functions are used to expose an API of your site's functionality.

HTTP functions allow you to expose the functionality of your site as a service. That means other people can write code to consume the functionality you expose by calling the functions of an API that you define.

To add an HTTP function, add a file named http-functions.js to the Backend section of your site. The code for your HTTP functions is added to that file.

HTTP functions are defined using the following pattern:

IGV4cG9ydCBmdW5jdGlvbiAmbHQ7cHJlZml4Jmd0O18mbHQ7ZnVuY3Rpb25OYW1lJmd0OyhyZXF1ZXN0KSB7IH0=
 export function <prefix>_<functionName>(request) { }

Where <prefix> is one of get, post, put, delete, or use and <functionName> is the name users use to reach your function. These are not functions that you call in your code, rather they are functions that you define. They are called when your users make HTTP requests using the associated URLs as described below.

For example, the following code creates a function that responds to GET requests by querying a collection to find items based on the path of the request. If matching items are found, an OK response is returned. If no matches are found, a Not Found response is returned.

IGltcG9ydCB7b2ssIG5vdEZvdW5kLCBzZXJ2ZXJFcnJvcn0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKIGltcG9ydCB3aXhEYXRhIGZyb20gJ3dpeC1kYXRhJzsKCiAvLyBVUkwgbG9va3MgbGlrZToKIC8vIGh0dHBzOi8vbXlzaXRlLmNvbS9fZnVuY3Rpb25zL215RnVuY3Rpb24vSm9obi9Eb2UKIC8vIG9yOgogLy8gaHR0cHM6Ly91c2VyLndpeHNpdGUuY29tL215c2l0ZS9fZnVuY3Rpb25zL215RnVuY3Rpb24vSm9obi9Eb2UKIGV4cG9ydCBmdW5jdGlvbiBnZXRfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CiAgIGxldCBvcHRpb25zID0gewogICAgICJoZWFkZXJzIjogewogICAgICAgIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9qc29uIgogICAgIH0KICAgfTsKICAgLy8gcXVlcnkgYSBjb2xsZWN0aW9uIHRvIGZpbmQgbWF0Y2hpbmcgaXRlbXMKICAgcmV0dXJuIHdpeERhdGEucXVlcnkoIm15VXNlcnNDb2xsZWN0aW9uIikKICAgICAuZXEoImZpcnN0TmFtZSIsIHJlcXVlc3QucGF0aFswXSkKICAgICAuZXEoImxhc3ROYW1lIiwgcmVxdWVzdC5wYXRoWzFdKQogICAgIC5maW5kKCkKICAgICAudGhlbiggKHJlc3VsdHMpID0+IHsKICAgICAgIC8vIG1hdGNoaW5nIGl0ZW1zIHdlcmUgZm91bmQKICAgICAgIGlmKHJlc3VsdHMuaXRlbXMubGVuZ3RoID4gMCkgewogICAgICAgICBvcHRpb25zLmJvZHkgPSB7CiAgICAgICAgICAgIml0ZW1zIjogcmVzdWx0cy5pdGVtcwogICAgICAgICB9OwogICAgICAgICByZXR1cm4gb2sob3B0aW9ucyk7CiAgICAgICB9CiAgICAgICAvLyBubyBtYXRjaGluZyBpdGVtcyBmb3VuZAogICAgICAgb3B0aW9ucy5ib2R5ID0gewogICAgICAgICAiZXJyb3IiOiBgJHtyZXF1ZXN0LnBhdGhbMF19ICR7cmVxdWVzdC5wYXRoWzFdfSB3YXNuJ3QgZm91bmRgCiAgICAgICB9OwogICAgICAgcmV0dXJuIG5vdEZvdW5kKG9wdGlvbnMpOwogICAgIH0gKQogICAgIC8vIHNvbWV0aGluZyB3ZW50IHdyb25nCiAgICAgLmNhdGNoKCAoZXJyb3IpID0+IHsKICAgICAgIG9wdGlvbnMuYm9keSA9IHsKICAgICAgICAgImVycm9yIjogZXJyb3IKICAgICAgIH07CiAgICAgICByZXR1cm4gc2VydmVyRXJyb3Iob3B0aW9ucyk7CiAgICAgfSApOwogfQ==
 import {ok, notFound, serverError} from 'wix-http-functions';
 import wixData from 'wix-data';

 // URL looks like:
 // https://mysite.com/_functions/myFunction/John/Doe
 // or:
 // https://user.wixsite.com/mysite/_functions/myFunction/John/Doe
 export function get_myFunction(request) {
   let options = {
     "headers": {
       "Content-Type": "application/json"
     }
   };
   // query a collection to find matching items
   return wixData.query("myUsersCollection")
     .eq("firstName", request.path[0])
     .eq("lastName", request.path[1])
     .find()
     .then( (results) => {
       // matching items were found
       if(results.items.length > 0) {
         options.body = {
           "items": results.items
         };
         return ok(options);
       }
       // no matching items found
       options.body = {
         "error": `${request.path[0]} ${request.path[1]} wasn't found`
       };
       return notFound(options);
     } )
     // something went wrong
     .catch( (error) => {
       options.body = {
         "error": error
       };
       return serverError(options);
     } );
 }

Clients consume your HTTP functions by reaching endpoints with the following patterns.

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLyZsdDtmdW5jdGlvbk5hbWUmZ3Q7
 https://{user_domain}/_functions/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>

You test your HTTP functions by reaching endpoints using the following patterns.

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLWRldi8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_domain}/_functions-dev/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy1kZXYvJmx0O2Z1bmN0aW9uTmFtZSZndDs=
 https://{user_name}.wixsite.com/{site_name}/_functions-dev/<functionName>

Contents

WixHttpFunctionRequest An object representing an incoming request received by a call to an HTTP function.
WixHttpFunctionResponse An object representing a response to an HTTP function request.
badRequest( ) Returns a response with status code 400 (Bad Request) and the information from the options parameter.
created( ) Returns a response with status code 201 (Created) and the information from the options parameter.
delete( ) A function that responds to requests made with the HTTP DELETE method.
forbidden( ) Returns a response with status code 403 (Forbidden) and the information from the options parameter.
get( ) A function that responds to requests made with the HTTP GET method.
notFound( ) Returns a response with status code 404 (Not Found) and the information from the options parameter.
ok( ) Returns a response with status code 200 (OK) and the information from the options parameter.
post( ) A function that responds to requests made with the HTTP POST method.
put( ) A function that responds to requests made with the HTTP PUT method.
response( ) Returns a response populated with the information from the options parameter. The response() function creates a custom response built with the information passed to the options parameter in a WixHttpFunctionResponseOptions object. Use the response() function to create a response to return from an HTTP function.
serverError( ) Returns a response with status code 500 (Internal Server Error) and the information from the options parameter.
use( ) A function that responds to requests made with any HTTP method.
WixHttpFunctionResponseOptions An object used to customize a response to an HTTP function call.
badRequest( )

badRequest( )

Returns a response with status code 400 (Bad Request) and the information from the options parameter.

import {badRequest} from 'wix-http-functions';
badRequest([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Description

The badRequest() function creates a response with the status code 400 (Bad Request).

Optionally, the badRequest() function can take a WixHttpFunctionResponseOptions object which is used to specify the response's body and headers. If the object contains a status it will be ignored.

Use the badRequest() function to create a response to return from an HTTP function. A 400 (Bad Request) response is usually used to indicate the request was unsuccessful because of a client error, such as a request using the incorrent syntax.

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a 400 (Bad Request) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7YmFkUmVxdWVzdH0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKCmV4cG9ydCBmdW5jdGlvbiB1c2VfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CgoJcmV0dXJuIGJhZFJlcXVlc3QoKTsKfQo=
// In http-functions.js

import {badRequest} from 'wix-http-functions';

export function use_myFunction(request) {

	return badRequest();
}

Create a 400 (Bad Request) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7YmFkUmVxdWVzdH0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKCmV4cG9ydCBmdW5jdGlvbiB1c2VfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CgoJbGV0IG9wdGlvbnMgPSB7CgkJYm9keTogewoJICAgICAgImVycm9yIjogIlRoZSByZXF1ZXN0IG11c3QgY29udGFpbiB0aGUgJ1gnIHBhcmFtZXRlci4iLAoJICAgIH0sCgkgICAgaGVhZGVyczogewoJICAgICAgIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9qc29uIgoJICAgIH0KCX07CgoJcmV0dXJuIGJhZFJlcXVlc3Qob3B0aW9ucyk7Cn0K
// In http-functions.js

import {badRequest} from 'wix-http-functions';

export function use_myFunction(request) {

	let options = {
		body: {
	      "error": "The request must contain the 'X' parameter.",
	    },
	    headers: {
	      "Content-Type": "application/json"
	    }
	};

	return badRequest(options);
}
created( )

created( )

Returns a response with status code 201 (Created) and the information from the options parameter.

import {created} from 'wix-http-functions';
created([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Description

The created() function creates a response with the status code 201 (Created).

Optionally, the created() function can take a WixHttpFunctionResponseOptions object which is used to specify the response's body and headers. If the object contains a status it will be ignored.

Use the created() function to create a response to return from an HTTP function. A 201 (Created) response is usually used to indicate that the request was successful and a new resource has been created.

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a 201 (Created) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7Y3JlYXRlZH0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKCmV4cG9ydCBmdW5jdGlvbiB1c2VfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CgogIHJldHVybiBjcmVhdGVkKCk7Cn0K
// In http-functions.js

import {created} from 'wix-http-functions';

export function use_myFunction(request) {

  return created();
}

Create a 201 (Created) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7Y3JlYXRlZH0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKCmV4cG9ydCBmdW5jdGlvbiB1c2VfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CgogIGxldCBvcHRpb25zID0gewogICAgYm9keTogewogICAgICAia2V5MSI6ICJ2YWx1ZTEiLAogICAgICAia2V5MiI6ICJ2YWx1ZTIiCiAgICB9LAogICAgaGVhZGVyczogewogICAgICAiQ29udGVudC1UeXBlIjogImFwcGxpY2F0aW9uL2pzb24iCiAgICB9CiAgfTsKCiAgcmV0dXJuIGNyZWF0ZWQob3B0aW9ucyk7Cn0K
// In http-functions.js

import {created} from 'wix-http-functions';

export function use_myFunction(request) {

  let options = {
    body: {
      "key1": "value1",
      "key2": "value2"
    },
    headers: {
      "Content-Type": "application/json"
    }
  };

  return created(options);
}
delete( )

delete( )

A function that responds to requests made with the HTTP DELETE method.

export function delete_<functionName>(request: wix-http-functions.WixHttpFunctionRequest): WixHttpFunctionResponse

Description

The HTTP DELETE method is usually called by consumers to delete an existing resource. If defined in this way and the resource is deleted, the function should respond with a 200 (OK) status code.

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

The delete() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All DELETE requests with the following URL will be routed to this function:

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLyZsdDtmdW5jdGlvbk5hbWUmZ3Q7
 https://{user_domain}/_functions/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>

Parameters

request WixHttpFunctionRequest The request object.

Return Value

WixHttpFunctionResponse

Examples

Create a DELETE HTTP function

This example creates a DELETE HTTP function named myFunction.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2t9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgovLyBVUkwgbG9va3MgbGlrZToKLy8gaHR0cHM6Ly9teXNpdGUuY29tL19mdW5jdGlvbnMvbXlGdW5jdGlvbi9zb21lSWQKLy8gb3I6Ci8vIGh0dHBzOi8vdXNlci53aXhzaXRlLmNvbS9teXNpdGUvX2Z1bmN0aW9ucy9teUZ1bmN0aW9uL3NvbWVJZApleHBvcnQgZnVuY3Rpb24gZGVsZXRlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKCWNvbnN0IHRvUmVtb3ZlID0gcmVxdWVzdC5wYXRoWzBdOwoKCS8vIHJlbW92ZSB0aGUgcmVzb3VyY2UKCglyZXR1cm4gb2soKTsKfQo=
// In http-functions.js

import {ok} from 'wix-http-functions';

// URL looks like:
// https://mysite.com/_functions/myFunction/someId
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/someId
export function delete_myFunction(request) {

	const toRemove = request.path[0];

	// remove the resource

	return ok();
}

Create a DELETE HTTP function

This example creates a DELETE HTTP function named myFunction that deletes an item from a collection named myCollection based on the path of the request. If the item is deleted successfully, an OK response is returned.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2ssIHNlcnZlckVycm9yfSBmcm9tICd3aXgtaHR0cC1mdW5jdGlvbnMnOwppbXBvcnQgd2l4RGF0YSBmcm9tICd3aXgtZGF0YSc7CgovLyBVUkwgbG9va3MgbGlrZToKLy8gaHR0cHM6Ly9teXNpdGUuY29tL19mdW5jdGlvbnMvbXlGdW5jdGlvbi83NmJmLThiZmEtNzE0Ci8vIG9yOgovLyBodHRwczovL3VzZXIud2l4c2l0ZS5jb20vbXlzaXRlL19mdW5jdGlvbnMvbXlGdW5jdGlvbi83NmJmLThiZmEtNzE0CmV4cG9ydCBmdW5jdGlvbiBkZWxldGVfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CglsZXQgb3B0aW9ucyA9IHsKCQkiaGVhZGVycyI6IHsKCQkJIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9qc29uIgoJCX0KCX07CgkvLyBkZWxldGUgdGhlIGl0ZW0gZnJvbSBhIGNvbGxlY3Rpb24KCXJldHVybiB3aXhEYXRhLnJlbW92ZSgibXlDb2xsZWN0aW9uIiwgcmVxdWVzdC5wYXRoWzBdKQoJICAudGhlbiggKHJlc3VsdHMpID0+IHsKICAgIAlvcHRpb25zLmJvZHkgPSB7CiAgICAJCSJkZWxldGVkIjogcmVzdWx0cwoJCQl9OwogICAgCXJldHVybiBvayhvcHRpb25zKTsKCSAgfSApCgkJLy8gc29tZXRoaW5nIHdlbnQgd3JvbmcKCSAgLmNhdGNoKCAoZXJyb3IpID0+IHsKCQkJb3B0aW9ucy5ib2R5ID0gewoJCQkJImVycm9yIjogZXJyb3IKCQkJfTsKCSAgICByZXR1cm4gc2VydmVyRXJyb3Iob3B0aW9ucyk7CgkgIH0gKTsKfQo=
// In http-functions.js

import {ok, serverError} from 'wix-http-functions';
import wixData from 'wix-data';

// URL looks like:
// https://mysite.com/_functions/myFunction/76bf-8bfa-714
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/76bf-8bfa-714
export function delete_myFunction(request) {
	let options = {
		"headers": {
			"Content-Type": "application/json"
		}
	};
	// delete the item from a collection
	return wixData.remove("myCollection", request.path[0])
	  .then( (results) => {
    	options.body = {
    		"deleted": results
			};
    	return ok(options);
	  } )
		// something went wrong
	  .catch( (error) => {
			options.body = {
				"error": error
			};
	    return serverError(options);
	  } );
}
forbidden( )

forbidden( )

Returns a response with status code 403 (Forbidden) and the information from the options parameter.

import {forbidden} from 'wix-http-functions';
forbidden([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Description

The forbidden() function creates a response with the status code 403 (Forbidden).

Optionally, the forbidden() function can take a WixHttpFunctionResponseOptions object which is used to specify the response's body and headers. If the object contains a status it will be ignored.

Use the forbidden() function to create a response to return from an HTTP function. A 403 (Forbidden) response is used to indicate the request was valid but the server is refusing to process it, usually because the client does not have the necessary permissions for the requested resource.

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a 403 (Forbidden) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7Zm9yYmlkZGVufSBmcm9tICd3aXgtaHR0cC1mdW5jdGlvbnMnOwoKZXhwb3J0IGZ1bmN0aW9uIHVzZV9teUZ1bmN0aW9uKHJlcXVlc3QpIHsKCiAgcmV0dXJuIGZvcmJpZGRlbigpOwp9Cg==
// In http-functions.js

import {forbidden} from 'wix-http-functions';

export function use_myFunction(request) {

  return forbidden();
}

Create a 403 (Forbidden) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7Zm9yYmlkZGVufSBmcm9tICd3aXgtaHR0cC1mdW5jdGlvbnMnOwoKZXhwb3J0IGZ1bmN0aW9uIHVzZV9teUZ1bmN0aW9uKHJlcXVlc3QpIHsKCiAgbGV0IG9wdGlvbnMgPSB7CiAgICBib2R5OiB7CiAgICAgICJlcnJvciI6ICJmb3JiaWRkZW4iLAogICAgfSwKICAgIGhlYWRlcnM6IHsKICAgICAgIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9qc29uIgogICAgfQogIH07CgogIHJldHVybiBmb3JiaWRkZW4ob3B0aW9ucyk7Cn0K
// In http-functions.js

import {forbidden} from 'wix-http-functions';

export function use_myFunction(request) {

  let options = {
    body: {
      "error": "forbidden",
    },
    headers: {
      "Content-Type": "application/json"
    }
  };

  return forbidden(options);
}
get( )

get( )

A function that responds to requests made with the HTTP GET method.

export function get_<functionName>(request: wix-http-functions.WixHttpFunctionRequest): WixHttpFunctionResponse

Description

The HTTP GET method is usually called by consumers to retrieve a resource and should have no other effect. If used in this way and the resource is found, the function should respond with a 200 (OK) status code and the requested resource.

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

The get() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All GET requests with the following URL will be routed to this function:

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLyZsdDtmdW5jdGlvbk5hbWUmZ3Q7
 https://{user_domain}/_functions/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>

Parameters

request WixHttpFunctionRequest The request object.

Return Value

WixHttpFunctionResponse

Examples

Create a GET HTTP function

This example creates a GET HTTP function named myFunction.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2ssIG5vdEZvdW5kfSBmcm9tICd3aXgtaHR0cC1mdW5jdGlvbnMnOwoKLy8gVVJMIGxvb2tzIGxpa2U6Ci8vIGh0dHBzOi8vbXlzaXRlLmNvbS9fZnVuY3Rpb25zL215RnVuY3Rpb24vZmluZE1lCi8vIG9yOgovLyBodHRwczovL3VzZXIud2l4c2l0ZS5jb20vbXlzaXRlL19mdW5jdGlvbnMvbXlGdW5jdGlvbi9maW5kTWUKZXhwb3J0IGZ1bmN0aW9uIGdldF9teUZ1bmN0aW9uKHJlcXVlc3QpIHsKCWlmKHJlcXVlc3QucGF0aFswXSA9PT0gImZpbmRNZSIpIHsKCQljb25zdCBib2R5ID0gIkZvdW5kIGl0ISI7CgkJcmV0dXJuIG9rKHtib2R5OiBib2R5fSk7Cgl9CgogIGNvbnN0IGJvZHkgPSAiQ2FuJ3QgZmluZCBpdCEiOwogIHJldHVybiBub3RGb3VuZCh7Ym9keTogYm9keX0pOwp9Cg==
// In http-functions.js

import {ok, notFound} from 'wix-http-functions';

// URL looks like:
// https://mysite.com/_functions/myFunction/findMe
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/findMe
export function get_myFunction(request) {
	if(request.path[0] === "findMe") {
		const body = "Found it!";
		return ok({body: body});
	}

  const body = "Can't find it!";
  return notFound({body: body});
}

Create a GET HTTP function

This example creates a GET HTTP function named myFunction that queries a collection named myCollection to find items based on the path of the request. If matching items are found, an OK response is returned. If no matches are found, a Not Found response is returned.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2ssIG5vdEZvdW5kLCBzZXJ2ZXJFcnJvcn0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKaW1wb3J0IHdpeERhdGEgZnJvbSAnd2l4LWRhdGEnOwoKLy8gVVJMIGxvb2tzIGxpa2U6Ci8vIGh0dHBzOi8vbXlzaXRlLmNvbS9fZnVuY3Rpb25zL215RnVuY3Rpb24vSm9obi9Eb2UKLy8gb3I6Ci8vIGh0dHBzOi8vdXNlci53aXhzaXRlLmNvbS9teXNpdGUvX2Z1bmN0aW9ucy9teUZ1bmN0aW9uL0pvaG4vRG9lCmV4cG9ydCBmdW5jdGlvbiBnZXRfbXlGdW5jdGlvbihyZXF1ZXN0KSB7CglsZXQgb3B0aW9ucyA9IHsKCQkiaGVhZGVycyI6IHsKCQkJIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9qc29uIgoJCX0KCX07CgkvLyBxdWVyeSBhIGNvbGxlY3Rpb24gdG8gZmluZCBtYXRjaGluZyBpdGVtcwoJcmV0dXJuIHdpeERhdGEucXVlcnkoIm15VXNlcnNDb2xsZWN0aW9uIikKICAJLmVxKCJmaXJzdE5hbWUiLCByZXF1ZXN0LnBhdGhbMF0pCiAgCS5lcSgibGFzdE5hbWUiLCByZXF1ZXN0LnBhdGhbMV0pCiAgCS5maW5kKCkKICAJLnRoZW4oIChyZXN1bHRzKSA9PiB7CgkJCS8vIG1hdGNoaW5nIGl0ZW1zIHdlcmUgZm91bmQKICAgIAlpZihyZXN1bHRzLml0ZW1zLmxlbmd0aCA+IDApIHsKICAgIAkJb3B0aW9ucy5ib2R5ID0gewogICAgCQkJIml0ZW1zIjogcmVzdWx0cy5pdGVtcwogIAkJCX07CiAgICAJCXJldHVybiBvayhvcHRpb25zKTsKICAgIAl9CgkJCS8vIG5vIG1hdGNoaW5nIGl0ZW1zIGZvdW5kCiAgCQlvcHRpb25zLmJvZHkgPSB7CiAgCQkJImVycm9yIjogYCcke3JlcXVlc3QucGF0aFswXX0gJHtyZXF1ZXN0LnBhdGhbMV19JyB3YXMgbm90IGZvdW5kYAoJCSAgfTsKICAJCXJldHVybiBub3RGb3VuZChvcHRpb25zKTsKICAJfSApCgkJLy8gc29tZXRoaW5nIHdlbnQgd3JvbmcKICAJLmNhdGNoKCAoZXJyb3IpID0+IHsKCQkJb3B0aW9ucy5ib2R5ID0gewoJCQkJImVycm9yIjogZXJyb3IKCQkJfTsKICAgIAlyZXR1cm4gc2VydmVyRXJyb3Iob3B0aW9ucyk7CiAgCX0gKTsKfQo=
// In http-functions.js

import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';

// URL looks like:
// https://mysite.com/_functions/myFunction/John/Doe
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/John/Doe
export function get_myFunction(request) {
	let options = {
		"headers": {
			"Content-Type": "application/json"
		}
	};
	// query a collection to find matching items
	return wixData.query("myUsersCollection")
  	.eq("firstName", request.path[0])
  	.eq("lastName", request.path[1])
  	.find()
  	.then( (results) => {
			// matching items were found
    	if(results.items.length > 0) {
    		options.body = {
    			"items": results.items
  			};
    		return ok(options);
    	}
			// no matching items 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);
  	} );
}
notFound( )

notFound( )

Returns a response with status code 404 (Not Found) and the information from the options parameter.

import {notFound} from 'wix-http-functions';
notFound([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Description

The notFound() function creates a response with the status code 404 (Not Found).

Optionally, the notFound() function can take a WixHttpFunctionResponseOptions object which is used to specify the response's body and headers. If the object contains a status it will be ignored.

Use the notFound() function to create a response to return from an HTTP function. A 404 (Not Found) response is usually used to indicate that the requested was not found at the current time.

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a 404 (Not Found) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7bm90Rm91bmR9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICByZXR1cm4gbm90Rm91bmQoKTsKfQo=
// In http-functions.js

import {notFound} from 'wix-http-functions';

export function use_myFunction(request) {

  return notFound();
}

Create a 404 (Not Found) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7bm90Rm91bmR9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICBsZXQgb3B0aW9ucyA9IHsKICAgIGJvZHk6IHsKICAgICAgImVycm9yIjogIm5vdCBmb3VuZCIKICAgIH0sCiAgICBoZWFkZXJzOiB7CiAgICAgICJDb250ZW50LVR5cGUiOiAiYXBwbGljYXRpb24vanNvbiIKICAgIH0KICB9OwoKICByZXR1cm4gbm90Rm91bmQob3B0aW9ucyk7Cn0K
// In http-functions.js

import {notFound} from 'wix-http-functions';

export function use_myFunction(request) {

  let options = {
    body: {
      "error": "not found"
    },
    headers: {
      "Content-Type": "application/json"
    }
  };

  return notFound(options);
}
ok( )

ok( )

Returns a response with status code 200 (OK) and the information from the options parameter.

import {ok} from 'wix-http-functions';
ok([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Description

The ok() function creates a response with the status code 200 (OK).

Optionally, the ok() function can take a WixHttpFunctionResponseOptions object which is used to specify the response's body and headers. If the object contains a status it will be ignored.

Use the response() function to create a response to return from an HTTP function. A 200 (OK) response is usually used to indicate that a request was successful.

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a 200 (OK) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2t9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICByZXR1cm4gb2soKTsKfQo=
// In http-functions.js

import {ok} from 'wix-http-functions';

export function use_myFunction(request) {

  return ok();
}

Create a 200 (OK) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2t9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICBsZXQgb3B0aW9ucyA9IHsKICAgIGJvZHk6IHsKICAgICAgImtleTEiOiAidmFsdWUxIiwKICAgICAgImtleTIiOiAidmFsdWUyIgogICAgfSwKICAgIGhlYWRlcnM6IHsKICAgICAgIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9qc29uIgogICAgfQogIH07CgogIHJldHVybiBvayhvcHRpb25zKTsKfQo=
// In http-functions.js

import {ok} from 'wix-http-functions';

export function use_myFunction(request) {

  let options = {
    body: {
      "key1": "value1",
      "key2": "value2"
    },
    headers: {
      "Content-Type": "application/json"
    }
  };

  return ok(options);
}
post( )

post( )

A function that responds to requests made with the HTTP POST method.

export function post_<functionName>(request: wix-http-functions.WixHttpFunctionRequest): WixHttpFunctionResponse

Description

The HTTP POST method is usually called by consumers to create a new resource. If defined in this way and the resource is created, the function should respond with a 201 (Created) status code and usually a reference to the new resource.

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

The post() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All POST requests with the following URL will be routed to this function:

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLyZsdDtmdW5jdGlvbk5hbWUmZ3Q7
 https://{user_domain}/_functions/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>

Parameters

request WixHttpFunctionRequest The request object.

Return Value

WixHttpFunctionResponse

Examples

Create a POST HTTP function

This example creates a POST HTTP function named myFunction.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7Y3JlYXRlZH0gZnJvbSAnd2l4LWh0dHAtZnVuY3Rpb25zJzsKCi8vIFVSTCBsb29rcyBsaWtlOgovLyBodHRwczovL215c2l0ZS5jb20vX2Z1bmN0aW9ucy9teUZ1bmN0aW9uLwovLyBvcjoKLy8gaHR0cHM6Ly91c2VyLndpeHNpdGUuY29tL215c2l0ZS9fZnVuY3Rpb25zL215RnVuY3Rpb24vCmV4cG9ydCBmdW5jdGlvbiBwb3N0X215RnVuY3Rpb24ocmVxdWVzdCkgewoKICByZXR1cm4gcmVxdWVzdC5ib2R5LnRleHQoKQogICAgLnRoZW4oIChib2R5KSA9PiB7CgogICAgICAvLyBpbnNlcnQgdGhlIGluZm8gZnJvbSB0aGUgYm9keSBzb21ld2hlcmUKCiAgICAgIHJldHVybiBjcmVhdGVkKCk7CiAgICB9ICk7Cn0K
// In http-functions.js

import {created} from 'wix-http-functions';

// URL looks like:
// https://mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/
export function post_myFunction(request) {

  return request.body.text()
    .then( (body) => {

      // insert the info from the body somewhere

      return created();
    } );
}

Create a POST HTTP function

This example creates a POST HTTP function named myFunction that inserts an item from the request's body into a collection named myCollection. If the item is inserted successfully, a Created response is returned.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7Y3JlYXRlZCwgc2VydmVyRXJyb3J9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CmltcG9ydCB3aXhEYXRhIGZyb20gJ3dpeC1kYXRhJzsKCi8vIFVSTCBsb29rcyBsaWtlOgovLyBodHRwczovL215c2l0ZS5jb20vX2Z1bmN0aW9ucy9teUZ1bmN0aW9uLwovLyBvcjoKLy8gaHR0cHM6Ly91c2VyLndpeHNpdGUuY29tL215c2l0ZS9fZnVuY3Rpb25zL215RnVuY3Rpb24vCmV4cG9ydCBmdW5jdGlvbiBwb3N0X215RnVuY3Rpb24ocmVxdWVzdCkgewoJbGV0IG9wdGlvbnMgPSB7CgkJImhlYWRlcnMiOiB7CgkJCSJDb250ZW50LVR5cGUiOiAiYXBwbGljYXRpb24vanNvbiIKCQl9Cgl9OwoJLy8gZ2V0IHRoZSByZXF1ZXN0IGJvZHkKCXJldHVybiByZXF1ZXN0LmJvZHkuanNvbigpCgkJLnRoZW4oIChib2R5KSA9PiB7CgkJCS8vIGluc2VydCB0aGUgaXRlbSBpbiBhIGNvbGxlY3Rpb24KICAgIAlyZXR1cm4gd2l4RGF0YS5pbnNlcnQoIm15Q29sbGVjdGlvbiIsIGJvZHkpOwogIAl9ICkKCSAgLnRoZW4oIChyZXN1bHRzKSA9PiB7CiAgICAJb3B0aW9ucy5ib2R5ID0gewogICAgCQkiaW5zZXJ0ZWQiOiByZXN1bHRzCgkJCX07CiAgICAJcmV0dXJuIGNyZWF0ZWQob3B0aW9ucyk7CgkgIH0gKQoJCS8vIHNvbWV0aGluZyB3ZW50IHdyb25nCgkgIC5jYXRjaCggKGVycm9yKSA9PiB7CgkJCW9wdGlvbnMuYm9keSA9IHsKCQkJCSJlcnJvciI6IGVycm9yCgkJCX07CgkgICAgcmV0dXJuIHNlcnZlckVycm9yKG9wdGlvbnMpOwoJICB9ICk7Cn0K
// In http-functions.js

import {created, serverError} from 'wix-http-functions';
import wixData from 'wix-data';

// URL looks like:
// https://mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/
export function post_myFunction(request) {
	let options = {
		"headers": {
			"Content-Type": "application/json"
		}
	};
	// get the request body
	return request.body.json()
		.then( (body) => {
			// insert the item in a collection
    	return wixData.insert("myCollection", body);
  	} )
	  .then( (results) => {
    	options.body = {
    		"inserted": results
			};
    	return created(options);
	  } )
		// something went wrong
	  .catch( (error) => {
			options.body = {
				"error": error
			};
	    return serverError(options);
	  } );
}
put( )

put( )

A function that responds to requests made with the HTTP PUT method.

export function put_<functionName>(request: wix-http-functions.WixHttpFunctionRequest): WixHttpFunctionResponse

Description

The HTTP PUT method is usually called by consumers to update an existing resource.

If defined in this way and the resource is updated, the function should respond with a 200 (OK) status code. If defined in this way and the resource is created because it did not exist, you should respond with a 201 (Created) status code.

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

The put() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All PUT requests with the following URL will be routed to this function:

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLyZsdDtmdW5jdGlvbk5hbWUmZ3Q7
 https://{user_domain}/_functions/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>

Parameters

request WixHttpFunctionRequest The request object.

Return Value

WixHttpFunctionResponse

Examples

Create a PUT HTTP function

This example creates a PUT HTTP function named myFunction.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2t9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgovLyBVUkwgbG9va3MgbGlrZToKLy8gaHR0cHM6Ly9teXNpdGUuY29tL19mdW5jdGlvbnMvbXlGdW5jdGlvbi8KLy8gb3I6Ci8vIGh0dHBzOi8vdXNlci53aXhzaXRlLmNvbS9teXNpdGUvX2Z1bmN0aW9ucy9teUZ1bmN0aW9uLwpleHBvcnQgZnVuY3Rpb24gcHV0X215RnVuY3Rpb24ocmVxdWVzdCkgewoKICByZXR1cm4gcmVxdWVzdC5ib2R5LnRleHQoKQogICAgLnRoZW4oIChib2R5KSA9PiB7CgogICAgICAvLyB1cGRhdGUgdGhlIGluZm8gZnJvbSB0aGUgYm9keSBzb21ld2hlcmUKICAgICAgcmV0dXJuIG9rKCk7CiAgICB9ICk7Cn0K
// In http-functions.js

import {ok} from 'wix-http-functions';

// URL looks like:
// https://mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/
export function put_myFunction(request) {

  return request.body.text()
    .then( (body) => {

      // update the info from the body somewhere
      return ok();
    } );
}

Create a PUT HTTP function

This example creates a PUT HTTP function named myFunction that updates an item from the request's body in a collection named myCollection. If the item is updated successfully, an OK response is returned.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2ssIHNlcnZlckVycm9yfSBmcm9tICd3aXgtaHR0cC1mdW5jdGlvbnMnOwppbXBvcnQgd2l4RGF0YSBmcm9tICd3aXgtZGF0YSc7CgovLyBVUkwgbG9va3MgbGlrZToKLy8gaHR0cHM6Ly9teXNpdGUuY29tL19mdW5jdGlvbnMvbXlGdW5jdGlvbi8KLy8gb3I6Ci8vIGh0dHBzOi8vdXNlci53aXhzaXRlLmNvbS9teXNpdGUvX2Z1bmN0aW9ucy9teUZ1bmN0aW9uLwpleHBvcnQgZnVuY3Rpb24gcHV0X215RnVuY3Rpb24ocmVxdWVzdCkgewoJbGV0IG9wdGlvbnMgPSB7CgkJImhlYWRlcnMiOiB7CgkJCSJDb250ZW50LVR5cGUiOiAiYXBwbGljYXRpb24vanNvbiIKCQl9Cgl9OwoJLy8gZ2V0IHRoZSByZXF1ZXN0IGJvZHkKCXJldHVybiByZXF1ZXN0LmJvZHkuanNvbigpCgkJLnRoZW4oIChib2R5KSA9PiB7CgkJCS8vIHVwZGF0ZSB0aGUgaXRlbSBpbiBhIGNvbGxlY3Rpb24KICAgIAlyZXR1cm4gd2l4RGF0YS51cGRhdGUoIm15Q29sbGVjdGlvbiIsIGJvZHkpOwogIAl9ICkKCSAgLnRoZW4oIChyZXN1bHRzKSA9PiB7CiAgICAJb3B0aW9ucy5ib2R5ID0gewogICAgCQkiaW5zZXJ0ZWQiOiByZXN1bHRzCgkJCX07CiAgICAJcmV0dXJuIG9rKG9wdGlvbnMpOwoJICB9ICkKCQkvLyBzb21ldGhpbmcgd2VudCB3cm9uZwoJICAuY2F0Y2goIChlcnJvcikgPT4gewoJCQlvcHRpb25zLmJvZHkgPSB7CgkJCQkiZXJyb3IiOiBlcnJvcgoJCQl9OwoJICAgIHJldHVybiBzZXJ2ZXJFcnJvcihvcHRpb25zKTsKCSAgfSApOwp9Cg==
// In http-functions.js

import {ok, serverError} from 'wix-http-functions';
import wixData from 'wix-data';

// URL looks like:
// https://mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/
export function put_myFunction(request) {
	let options = {
		"headers": {
			"Content-Type": "application/json"
		}
	};
	// get the request body
	return request.body.json()
		.then( (body) => {
			// update the item in a collection
    	return wixData.update("myCollection", body);
  	} )
	  .then( (results) => {
    	options.body = {
    		"inserted": results
			};
    	return ok(options);
	  } )
		// something went wrong
	  .catch( (error) => {
			options.body = {
				"error": error
			};
	    return serverError(options);
	  } );
}
response( )

response( )

Returns a response populated with the information from the options parameter. The response() function creates a custom response built with the information passed to the options parameter in a WixHttpFunctionResponseOptions object.

Use the response() function to create a response to return from an HTTP function.

response([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7cmVzcG9uc2V9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICByZXR1cm4gcmVzcG9uc2UoKTsKfQo=
// In http-functions.js

import {response} from 'wix-http-functions';

export function use_myFunction(request) {

  return response();
}

Create a response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7cmVzcG9uc2V9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICBsZXQgb3B0aW9ucyA9IHsKICAgIHN0YXR1czogNDE4LAogICAgYm9keTogewogICAgICAia2V5MSI6ICJ2YWx1ZTEiLAogICAgICAia2V5MiI6ICJ2YWx1ZTIiCiAgICB9LAogICAgaGVhZGVyczogewogICAgICAiQ29udGVudC1UeXBlIjogImFwcGxpY2F0aW9uL2pzb24iCiAgICB9CiAgfTsKCiAgcmV0dXJuIHJlc3BvbnNlKG9wdGlvbnMpOwp9Cg==
// In http-functions.js

import {response} from 'wix-http-functions';

export function use_myFunction(request) {

  let options = {
    status: 418,
    body: {
      "key1": "value1",
      "key2": "value2"
    },
    headers: {
      "Content-Type": "application/json"
    }
  };

  return response(options);
}
serverError( )

serverError( )

Returns a response with status code 500 (Internal Server Error) and the information from the options parameter.

import {serverError} from 'wix-http-functions';
serverError([options: WixHttpFunctionResponseOptions]): WixHttpFunctionResponse

Description

The serverError() function creates a response with the status code 500 (Internal Server Error).

Optionally, the serverError() function can take a WixHttpFunctionResponseOptions object which is used to specify the response's body and headers. If the object contains a status it will be ignored.

Use the serverError() function to create a response to return from an HTTP function. A 500 (Internal Server Error) response is usually used to indicate the request was unsuccessful because of an unexpected error on the server.

Parameters

options WixHttpFunctionResponseOptions The response options.

Return Value

WixHttpFunctionResponse

Examples

Create a 500 (Internal Server Error) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7c2VydmVyRXJyb3J9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICByZXR1cm4gc2VydmVyRXJyb3Iob3B0aW9ucyk7Cn0K
// In http-functions.js

import {serverError} from 'wix-http-functions';

export function use_myFunction(request) {

  return serverError(options);
}

Create a 500 (Internal Server Error) response

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7c2VydmVyRXJyb3J9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKICBsZXQgb3B0aW9ucyA9IHsKICAgIGJvZHk6IHsKICAgICAgImVycm9yIjogImludGVybmFsIHNlcnZlciBlcnJvciIsCiAgICB9LAogICAgaGVhZGVyczogewogICAgICAiQ29udGVudC1UeXBlIjogImFwcGxpY2F0aW9uL2pzb24iCiAgICB9CiAgfQoKICByZXR1cm4gc2VydmVyRXJyb3Iob3B0aW9ucyk7Cn0K
// In http-functions.js

import {serverError} from 'wix-http-functions';

export function use_myFunction(request) {

  let options = {
    body: {
      "error": "internal server error",
    },
    headers: {
      "Content-Type": "application/json"
    }
  }

  return serverError(options);
}
use( )

use( )

A function that responds to requests made with any HTTP method.

export function use_<functionName>(request: wix-http-functions.WixHttpFunctionRequest): WixHttpFunctionResponse

Description

Requests made with any of the GET, POST, PUT, or DELETE HTTP methods will be routed to this function unless another function is defined specifically for the request's HTTP method.

For example, if you create functions named get_myFunction and use_myFunction, GET calls to myFunction will be handled by get_myFunction, while POST, PUT, and DELETE calls will be handled by use_myFunction.

The use() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All GET, POST, PUT, or DELETE HTTP requests with the following URL will be routed to this function unless another function is defined specifically for the request's HTTP method:

Premium sites:

IGh0dHBzOi8ve3VzZXJfZG9tYWlufS9fZnVuY3Rpb25zLyZsdDtmdW5jdGlvbk5hbWUmZ3Q7
 https://{user_domain}/_functions/<functionName>

Free sites:

IGh0dHBzOi8ve3VzZXJfbmFtZX0ud2l4c2l0ZS5jb20ve3NpdGVfbmFtZX0vX2Z1bmN0aW9ucy8mbHQ7ZnVuY3Rpb25OYW1lJmd0Ow==
 https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

Parameters

request WixHttpFunctionRequest The request object.

Return Value

WixHttpFunctionResponse

Examples

Create a catchall HTTP function

This example creates a catchall HTTP function named myFunction.

Ly8gSW4gaHR0cC1mdW5jdGlvbnMuanMKCmltcG9ydCB7b2t9IGZyb20gJ3dpeC1odHRwLWZ1bmN0aW9ucyc7CgpleHBvcnQgZnVuY3Rpb24gdXNlX215RnVuY3Rpb24ocmVxdWVzdCkgewoKCWNvbnN0IG1ldGhvZCA9IHJlcXVlc3QubWV0aG9kOwoKCS8vIC4uLgoKCXJldHVybiBvaygpOwp9Cg==
// In http-functions.js

import {ok} from 'wix-http-functions';

export function use_myFunction(request) {

	const method = request.method;

	// ...

	return ok();
}
WixHttpFunctionResponseOptions

WixHttpFunctionResponseOptions

An object used to customize a response to an HTTP function call.

Properties

status (optional) Number The response's HTTP status code.
body (optional) String |
Buffer
The response's body.
headers (optional) Object The response's header fields. The headers property contains an object of key:value pairs where the key is the header field name and the value is the header field value.