Active Web Elements Basic Examples

Other Documentation Resources
One of the simplest web applications one can deploy to the Wolfram Web Engine consists of a single expression among those that define active web elements, such as FormFunction, APIFunction, FormPage and others.
The documentation pages for these functions contain extensive examples, and there are guides for form creation, API development and an extensive tech note on advanced form creation.
From CloudDeploy to ServiceDeploy
Most of this documentation has been written using CloudDeploy and the Wolfram Cloud; however, ServiceDeploy has a very similar argument structure and most examples can be made to work in the Wolfram Web Engine by substituting the former with the latter and by adding the ServiceObject as the first argument.
For example, the first example of FormFunction reads:
One can easily replace it with:
Say, on the other hand, one wants to choose the location of this deployment:
Basic Forms
The most basic form of interaction between a user and a web server is through a form. Forms provide a means to input various kinds of data and closely match the concept of pure functions in Wolfram Language.
Given the parallelism between the functional nature of Wolfram Language and the design of HTML forms, it is generally very simple to transform code written to work in a notebook to be shared with friends and colleagues.
This is a simple form for factoring polynomials:
Forms can also be used to retrieve data built into Wolfram Language, for example the population of a city:
Basic APIs
Forms are best suited for interactions between a human and a web server, but sometimes the interaction is between two layers of an application or, in general, two different pieces of software. In such cases, it is better to have a more computable interface with machine-readable errors and structured output.
These are the design principles behind APIFunction, which essentially mirrors the argument structure of FormFunction but uses defaults that are more suitable for computation rather than human interaction.
The following API will compute the nth prime number:
Such an API can be evaluated in the command line using curl:
$ curl http://localhost:8080/webengine/prime?n=1000000000
22801763489
When no parameters are provided, the API returns a JSON file:
$ curl http://localhost:8080/webengine/prime
{
"Success":false,
"Failure":"The API could not be evaluated because there is no input for fields: \"n\".",
"Fields":{
"n":{
"AllowedExtensions":[
"json"
],
"AutoSubmitting":false,
"CodeLanguage":"Automatic",
"Default":null,
"Enabled":true,
"Failure":"This field is required.",
"Help":null,
"Hidden":false,
"Hint":null,
"Input":null,
"Interpreter":"Integer",
"Label":"n",
"Required":true,
"Type":"Element"
}
}
}
URLExecute automatically imports as a list of rules: