Functions
Functions add your own logic to an endpoint: validate the incoming data, complete a record, format a response… A function belongs to a project and can be plugged into any endpoint of that project, before or after the database query.
Where to find functions
Open your project: the Functions section lists the project's functions, with their name, version and runtime.
To find a function, type part of its name, version or runtime in Filter functions....
Create a function
- In the Functions section, click Add Function.
- Choose the Runtime:
- Javascript: you write the code directly in Grizzly;
- AWS Lambda: Grizzly calls a Lambda function already deployed in your AWS account.
- Enter the Function Name and the Version (
1.0.0by default). - Fill in the runtime-specific fields (see below).
- Click Save & Exit.
The name + version pair must be unique in the project. To change a function without affecting the endpoints that use it, create it under the same name with a new version.
Javascript
The Code editor contains a starter example:
// example javascript input function
(request) => {
return request.body;
}Your code must be an arrow function: Grizzly calls it on every request (see What a function receives).
AWS Lambda
Fill in:
| Field | Content |
|---|---|
| AWS Function Name | The name of the Lambda function to call |
| AWS_ACCESS_KEY_ID | The AWS access key ID |
| AWS_ACCESS_SECRET | The matching secret key |
| AWS REGION | The region where the function is deployed (US_EAST_1 by default) |
Dedicated access key
Use an AWS access key that is only allowed to invoke this function.
Plug a function into an endpoint
Functions are plugged in at the Functions step of the endpoint wizard (see Resource groups and endpoints):
- Input Function: runs before the database query;
- Output Function: runs after it, on the query result.
Pick a function in Choose a Function. Show the function displays its code without leaving the wizard. If the function doesn't exist yet, Create Function opens the same creation window.
What a function receives
Input function
It receives a request object that describes the call:
| Property | Content |
|---|---|
request.body | The request body (JSON) |
request.query | The Query parameters declared on the endpoint |
request.pathVariable | The Path parameters, for example reference for /products/{reference} |
request.header | The Header parameters declared on the endpoint |
For an insert or update endpoint, the object returned by the function is what gets saved in the database.
Output function
It receives two arguments: request (the same object) and response, the result of the database query. What it returns becomes the API's response.
Return an error
To stop the call with an HTTP code, return _throwException(message, code). The message is a string and the code an integer between 100 and 599:
return _throwException("Price must be positive", 400);Execution time
A Javascript function that runs for more than 10 seconds is stopped.
Example: the shop catalog
In the Shop project, an input function prepareProduct for POST /products rejects negative prices and adds a creation date:
(request) => {
const product = request.body;
if (product.price < 0) {
return _throwException("Price must be positive", 400);
}
product.createdAt = new Date().toISOString();
return product;
}An output function productSummary for GET /products only returns each product's reference and name:
(request, response) => {
return response.map(p => ({ reference: p.reference, name: p.name }));
}Then test the endpoints with Test and validate (see Test, export and share).
Edit or delete a function
On the function's card:
- Edit (✎) reopens the window with the current values. Finish with Save & Exit;
- Delete (trash) deletes the function, after confirmation in the Delete Function window.
What's next?
- Plug your functions into your endpoints: Resource groups and endpoints.
- Chain endpoint, logic and database visually with the Flow Builder.
- Control who can call your endpoints: Security.
