API functions
| Function | Description |
|---|---|
api::invoke() | Invokes an /api endpoint and returns the result |
api::timeout() | Middleware to set a timeout for requests made to a defined API endpoint |
api::req::body() | Middleware to set the body type for an API endpoint request |
api::res::body() | Middleware to set the body type for an API endpoint response |
api::res::header() | Middleware to add a single header to an API endpoint response |
api::res::headers() | Middleware to adds multiple headers to an API endpoint response |
api::res::status() | Middleware to set the status for an API endpoint response |
Custom middleware | Middleware to set the status for an API endpoint response |
Overview
API functions are passed in as middleware inside a DEFINE API or DEFINE CONFIG API statement and called a request is received.
The only API function intended for use in regular queries is the api::invoke function, which is used to test API endpoints instead of as middleware.
The signatures for all other functions are presented here are from the point of view of the user. For example, the api::timeout function takes a single duration.
In practice, two extra arguments are passed in to these functions unseen to the user, making this the true signature.
For more details on how this works, see this section of the page.
api::invoke
The api::invoke function invokes a custom /api endpoint defined using a DEFINE API statement. While a DEFINE API statement creates an API endpoint at the /api/:namespace/:database/:endpoint path, this function is called when a namespace and database have already been decided, necessitating only the final path (such as "/test") for it to be invoked.
The following two examples of the function assume that this DEFINE API statement has been used to set up the "/test" endpoint.
Calling the api::invoke function with just a path:
Calling the api::invoke function with a path and an object containing a body and headers:
For more information and examples, see the page for the DEFINE API statement.
api::timeout
The api::timeout function sets the maximum timeout for a request.
The following example will always return an error because the
api::req::body
This function sets the strategy (the input format) for the endpoint. It can take one of the following strings:
'auto'
'json'
'cbor'
'flatbuffers'
'plain'
'bytes'
'native'
The following example shows an endpoint for each of these strategies, followed by an invocation for each that matches it.
api::res::body
api::res::header
The api::res::header function sets a single header for a response.
api::res::headers
The api::res::headers function takes an object to set the headers for a response.
api::res::status
The api::res::status function adds a status to the response of an API endpoint.
Setting an invalid HTTP status will result in an error.
Custom middleware
A DEFINE FUNCTION statement can be used to define a function for use as custom middleware. For more details on defining a custom function in this manner, see the DEFINE API page.
Structure of API functions
An API function can technically be called in the same way as any other function, as long as the first argument is an object and the second argument is a closure that returns an object. After a MIDDLEWARE clause these arguments will be automatically filled, but dummy arguments can be passed in for practice or testing.