Skip to content

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 ​

  1. In the Functions section, click Add Function.
  2. Choose the Runtime:
    • Javascript: you write the code directly in Grizzly;
    • AWS Lambda: Grizzly calls a Lambda function already deployed in your AWS account.
  3. Enter the Function Name and the Version (1.0.0 by default).
  4. Fill in the runtime-specific fields (see below).
  5. 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:

js
// 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:

FieldContent
AWS Function NameThe name of the Lambda function to call
AWS_ACCESS_KEY_IDThe AWS access key ID
AWS_ACCESS_SECRETThe matching secret key
AWS REGIONThe 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:

PropertyContent
request.bodyThe request body (JSON)
request.queryThe Query parameters declared on the endpoint
request.pathVariableThe Path parameters, for example reference for /products/{reference}
request.headerThe 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:

js
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:

js
(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:

js
(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? ​

Grizzly API · CodeOnce Software