API Functions
Last updated
Was this helpful?
Last updated
Was this helpful?
Available since v1.4.0
API Functions enable you to create custom API endpoints using traditional .
An API Function is a type of Function that can be triggered by a HTTP request. Instead of a Task for a traditional Function, an API Function will receive the HTTP request object as its input. The API Function can create an output object which resolves to a HTTP response.
In this example you will learn how to create, deploy and execute an API Function.
The following code snippet illustrates a basic API function that returns the HTTP method used for the request in the response.
Here is an example of a request object received by an API Function as its input:
The requestContext.http.method
field represents the HTTP method used to target the API function. Currently this can be any of the following values: GET
, POST
, PUT
, PATCH
, DELETE
, HEAD
and OPTIONS
.
The rawPath
field refers to the path portion of the URL that comes after the Function name.
For the following URL:
The rawPath
value would be /hello/world
The rawQueryString
field represents the unprocessed query string of the incoming HTTP request including the starting question mark.
For the following URL:
The rawQueryString
value will be ?param1=value
The headers
field represents the headers of the incoming HTTP request. It is structured as an object, where each key-value pair corresponds to a header name and its value.
The header names within the headers
field are transformed to be all lower case, to ensure consistent and case-insensitive handling of headers.
Following headers are added by Extra Horizon when invoking the API Function:
x-exh-requesting-user-id
contains the id of the user making the request.
x-exh-requesting-application-id
contains the application id of the oAuth application
These x-exh-requesting-
headers are guarded by the platform and can be trusted to be correct.
The following HTTP request will create the resultant header object:
The isBase64Encoded
property indicates if the body
property of the request object has been Base64-encoded.
The body
field represents the body of the incoming HTTP request as string. It can be Base64-encoded based on the isBase64Encoded
property.
Always check the isBase64Encoded
property to know if the body
string needs to be Base64 decoded before usage.
No assumptions can be made for when the body is going to be Base64 encoded or not as it will vary.
Here is an example response object of an API Function:
The statusCode
field resolves to the status code in the HTTP response. The value can be between the 200
- 499
range. If a status code outside of that range is passed, an INVALID_API_FUNCTION_RESULT
error will be returned.
The headers
field resolves to the headers in the HTTP response. It is structured as an object, where each key-value pair corresponds to a header name and its value.
If no Content-Type
header is present in the output, application/json
will be set as the default value. It is recommend to always explicitly set a Content-Type
header in the response object.
Also for the Content-Type
header, charset
could automatically be added to the header value in some cases. For example text/plain
could resolve to text/plain; charset=utf-8
.
The Connection
and Content-Length
header values are ignored as they're automatically added to the response.
The body
field resolves to the body in the HTTP response.
In order to include binary data within a body, such as images or audio files, it is necessary to convert it into a Base64 string and assign it to the body
field of the response object. Additionally, the isBase64Encoded
field must be set to true
.
If the response body does not contain binary data, you can directly include the data in its original format.
If the isBase64Encoded
field is set to true
, any other value then a Base64 string in thebody
field will result in an INVALID_FUNCTION_RESULT
error.
The isBase64Encoded
field must be set to true
if the body
field of the response object is a Base64 encoded string representing the raw response body.
To enable users to execute the function, appropriate permissions can be granted.
One of the following values may be assigned to the permissionMode
property to enforce their respective execution restrictions.
allUsers
A user must be logged in, but no permission is required.
public
Any party may execute this function without any restrictions.
Permissions for executing an API function can be assigned two levels of granularity.
The EXECUTE_API_FUNCTION
permission allows a user to execute all API functions.
The EXECUTE_API_FUNCTION:<function_name>
permission allows a user to execute the API function specified by the <function_name>
.
An API function allows for direct control over the HTTP response. However standard errors may still occur. For instance the INVALID_FUNCTION_RESULT
error mentioned before.
It is possible to receive errors related to problematic code, such as invalid syntax or unhandled exceptions within the API Function. These present themselves as a LAMBDA_RUNTIME_EXCEPTION
including the original error.
Please note that we are not affiliated with any of the specific packages mentioned above.
Available since v1.5.0
Every request made to an API Function is logged as an API Request, similar to how normal Function executions are saved as Tasks. These API Requests provide valuable information about the executed requests. The logs that an API Function generates during its execution are saved as API Request Logs.
API Requests are a summary of the requests made to API Functions. They can be helpful to monitor and debug API Function calls.
API Request Properties
The functionName
refers to the name of the API Function that was invoked in the request.
The method
field represents the HTTP method used to target the API function.
The path
field refers to the portion of the URL that comes after the Function name. The query string will be included in the path.
For the following URL:
The path
value would be hello/world?param1=value
The userId
field refers to the id of the Extra Horizon user that has performed the request.
The applicationId
field refers to the id of the Extra Horizon oAuth application that has performed the request.
The statusCode
field refers to the status code of the API Function's response or of a thrown error during its lifecycle
The timestamp
field refers to the time the API Function was executed.
The duration
field is the duration of the API Function execution in seconds.
When an error occurs during an API Function Request the error object is logged.
error.type
The error type defines where the error occurred during the API Function lifecycle and can have one of the following values:
invocation
: errors that occur before invoking the API Function
runtime
: errors that occurred during the execution of the API Function
response
: errors that occur during the response validation after the API Function has been executed
error.name
The name of the caught error
error.message
The message of the caught error
API Functions may include logging statements during execution. These logs are saved as API Request Logs.
API Request Log Properties
The timestamp
field refers to the time the log was printed.
The message
property contains the message being printed.
The request and response sizes are both limited to 6 Megabytes
, after conversion to the standardized formats.
Unlike normal Functions API Functions are limited to a timeLimit
of 30 seconds.
Currently the API Functions only support the GET
, POST
, PUT
, DELETE
, PATCH
, OPTIONS
and HEAD
HTTP methods.
Currently the API Functions only support the status codes between 200 and 499.
The RQL that can be used on the API Requests Logs endpoint is limited to only the ge
operator in combination with the timestamp
field.
Missing API Requests
We prioritize providing the API Function result over saving the API Request. In the rare case that saving an API Request fails, no error message will be provided and the API Request record will be missing.
Similar to traditional Functions, API Functions can be deployed by referring to the documentation provided in the example.
API functions can be executed by making requests to the /tasks/v1/api/<function_name>
endpoint. This endpoint can be targeted using the common . The URL can be extended by appending additional path parameters and a query string.
can be used to execute the API Function using the following example:
Please refer to the for further information on setting up the SDK.
The API Function receives the HTTP request as its input. The request is transformed to the standardized (version 2.0). By using this standardized format, we ensure that the API Functions can handle HTTP requests consistently across different programming languages.
Depending on your cluster configuration, Extra Horizon will handle for you. Therefore, some OPTIONS
requests might not reach your API Function and additional headers could be added to the response you've generated.
An API Function is expected to generate an output object which will be resolved to the HTTP response. The output is defined by the standardized (version 2.0). By using this standardized format, we ensure that the API Functions can handle HTTP responses consistently across different programming languages.
The executionOptions.permissionMode
property of the determines the execution restrictions based on its assigned value.
permissionRequired
A user requires a permission as stated in the section. This is the default permission mode.
For further information regarding the application of permissions to users please refer to .
For a complete list of errors please refer to the .
As explained in the section about the , the body could be base64 encoded to support the reception of various content types. Consequently, to consume a received request within the function, it is necessary to pre-process the request body.
API functions offer a wide range of possibilities, including the ability to create full web services. The request is transformed to the standardized (version 2.0). By using this standardized format, allows the usage off existing 3th party packages to seamlessly integrate complex logic.
For instance when using the Node.js runtime, request routing can be implemented using a package like and a popular web framework. Below is an example of an as an API Function.
Managing Functions is greatly simplified by the A very basic API Function can be generated by the following command:
Please refer to the for further information on setting up the CLI.
can be used list API Requests using the following example:
can be used list API Request Logs using the following example: