Updated on 2026-01-08 GMT+08:00

Initializer Definition

This section describes how to define the initializer for the runtimes that support inline code editing.

Node.js

FunctionGraph supports the following Node.js runtimes:

  • Node.js 6.1 (runtime = Node.js6)
  • Node.js 8.9 (runtime = Node.js8)
  • Nodejs10.16(runtime = Node.js10)
  • Nodejs12.13(runtime = Node.js12)

Initializer syntax:

[File name].[Initializer name]

For example, if the initializer is named index.initializer, FunctionGraph loads the initializer function defined in the index.js file.

To use Node.js to build initialization logic, define a Node.js function as the initializer. The following is a simple initializer:

exports.initializer = function(context, callback) {
    callback(null, '');
    };
  • Function name

    The function name exports.initializer must be the initializer function name specified for a function.

    For example, if the initializer is named index.initializer, FunctionGraph loads the initializer function defined in the index.js file.

  • context

    The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.

  • callback

    The callback parameter is used to return the invocation result. The signature of this parameter is function(err, data), which is the same as that of the common callback parameter used in Node.js. If the value of err is not null, the function will return HandledInitializationError. The value of data is invalid because no value will be returned for function initialization. You can set the data parameter as null as it is set in the previous example.

Python

FunctionGraph supports the following Python runtimes:

  • Python 2.7 (runtime = python2.7)
  • Python 3.6 (runtime = python3)

Initializer syntax:

[File name].[Initializer name]

For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.py file.

To use Python to build initialization logic, define a Python function as the initializer. The following is a simple initializer:

def my_initializer(context):
    print("hello world!")
  • Function name

    The function name my_initializer must be the initializer function name specified for a function. For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.py file.

  • context

    The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.

PHP

FunctionGraph supports the following PHP runtime:

  • Php 7.2 (runtime = Php7.2)

Initializer syntax:

[File name].[Initializer name]

For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.php file.

To use PHP to build initialization logic, define a PHP function as the initializer. The following is a simple initializer:

<?php
Function my_initializer($context) {
    echo 'hello world' . PHP_EOL;
    }
?>
  • Function name

    The function name my_initializer must be the initializer function name specified for a function.

    For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.php file.

  • context

    The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.

Java

FunctionGraph supports the following Java runtime:

  • Java 8 (runtime = Java8)

    Initializer syntax:

    [Package name].[Class name].[Execution function name]

    For example, if the initializer is named com.Demo.my_initializer, FunctionGraph loads the my_initializer function defined in the com file.

    To use Java to build the initialization logic, define a Java function as the initializer. The following is a simple initializer:

    public void my_initializer(Context context)

    {

    RuntimeLogger log = context.getLogger();

    log.log(String.format("ak:%s", context.getAccessKey()));

    }

  • Function name

    The function name my_initializer must be the initializer function name specified for a function.

    For example, if the initializer is named com.Demo.my_initializer, FunctionGraph loads the my_initializer function defined in the com file.

  • context

    The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.