Items in APIScript

Version 25.3.9411


Items in APIScript


Feeds are composed of items, but in APIScript items are used for more than the individual parts of a feed. They are also used to represent inputs to operations.

Declare Items

In APIScript items are created, named, and given attribute values through the api:set keyword:

<api:set item="input" attr="mask" value="*.txt" />

The example above sets the mask attribute to the value *.txt on the item named input. In this case, the input item is like a variable in APIScript.

However, an item named input is never declared. Instead, the item is created the first time you try to set an attribute on it. In the previous example, if the input item did not already exist, it would be created and the mask attribute would be set on it.

Select Attribute Values

To reference an attribute, use the syntax item.attribute (for example, input.mask). To query an attribute value, surround the attribute name in square brackets ([ ]). This tells the interpreter that you want to evaluate the string instead of interpreting it as a string literal.

For example, consider this code snippet:

<api:set item="item1" attr="attr1" value="value1"/>
<api:set item="item1" attr="attr2" value="item1.attr1"/>
<api:set item="item1" attr="attr3" value="[item1.attr1]"/>

Here are the results:

  • item1.attr1 is assigned the literal string value value1.
  • item1.attr2 is assigned the literal string value item1.attr1.
  • item1.attr3 is assigned the value value1, because the string “[item1.attr1]” is evaluated at run time as the attr1 attribute of item1.

Note: You can use a backslash to escape square brackets in string literals.

Default Item

In APIScript there is always an implicit, unnamed item on an internal stack of items, the default item. In the following two cases, the default item is often used to make scripts shorter and easier to write:

  • Calling operations: The default item is the item passed by default to your script operation if no other item is specified. This means that you can use api:set to write attributes to the default unnamed item, and those attributes are passed as input to the next script operation called. This is one way to provide an operation’s required parameters.
  • Processing the current item in the output of an operation or script: If you do not specify a variable name for the result of an operation, then inside the api:call block that invokes the operation, the default item refers to the current item produced by the operation.

Manipulate the default item by omitting the item attribute of an APIScript keyword:

<api:set attr="path" value="." />

Special Items

In addition to items declared in the script, several built-in items are available in the scope of a script. Built-in, or special, items available in APIScript provide an interface for accessing parts of HTTP requests, responses, and more. These special items are useful for mapping inputs in HTTP requests to operation inputs.

The following sections detail the special items.

Script Inputs (_input)

The input for a script can be read from the _input item. The _input item contains variables found in the URL query string and the POST data when the script is executed. If a variable of the same name exists in the POST data, it overrides the value from the query string.

When you read values from the default item in a script, you are reading values from _input. Similarly, attributes that you write to the default item are passed as parameters to operations along with the input to the script. Only variables defined in the info block or in the script are available in the _input item.

Note: Inside an api:call block, _input is not the default item so you must reference it by name if you need access to it.

Script Outputs (_out[n])

You can access the current item in a feed produced by the api:call keyword through the default item or a named special item: _out[n], where n is the level of nesting of api:call keywords. For example, if you are inside a single api:call keyword, the item’s name is _out1. If you are inside three levels of nested api:call keywords, the name is _out3.

HTTP Request (_request)

You can access variables passed into the URL query string, POSTed to the script, and other variables as collections of attributes in the _request item. To access a specific collection of attributes, use the name of the collection as the prefix to the attribute you want to read. For example, [_request.qstring:name] reads the value of the name variable in the query string and [_request.form:name] reads the value of the name variable in the form data.

[query]string:* Access the values of query string parameters.
form:* Access the values of variables in the form data.
server:* Access the values of server variables.
body:* Access the raw contents (body) of the request.
auth:* Access authentication information about the user. Determine whether the user is authenticated with [_request.auth:isauthenticated].
other:* Access other information about the request. Retrieve the application path with [_request.other:applicationpath].

HTTP Response (_response)

The response item allows the script writer to write directly to the HTTP response. You can set the following attributes in the response item.

cookie:* Set cookies in the response. The name of the attribute is the name of the cookie prefixed by cookie: and the value of the attribute is the cookie value.
writefile Write the content of the specified path to the response.
redirect Send the redirect header to the client browser. Use this to redirect the browser to another URL.
statuscode Set the HTTP status code of the response.
statusdescription Set the HTTP status description of the response.

HTTP Headers (_httpheaders)

The _httpheaders item provides easy access to HTTP headers in scripts and templates. You can read the HTTP request headers by reading from this item. Write to the outgoing response headers by writing to this item. For example, you can set the content type with a line like the following:

<api:set item="_httpheaders" attr="content-type" value="application/xml"/>

HTTP Cookies (_cookies)

You can use the _cookies item to retrieve cookies in the request and set cookies in the response. A cookie with multiple key/value pairs is represented as a collection of attributes that correspond to the key/value pairs of the cookie. The name of the cookie prefixes each attribute of the collection.

ASP.NET Session (_session)

ASP.NET session variables are available in APIScript through the _session item. You can access any object stored in the session as attributes of this item. The attribute name represents the key used to store the object in the session.