Skip to main content

OpenFunction

The openfunction plugin makes a synchronous OpenFunction endpoint the effective upstream for a route. It forwards the client method, headers, query parameters, and body and returns the function response.

Examples​

The examples show how to invoke a synchronous function and preserve a wildcard route suffix.

Install OpenFunction by following its installation guide. Deploy the official Hello World Go sample by following its Deployment instructions. The sample accepts a greeting at /<greeting> and returns Hello, <greeting>!.

Obtain the function's internal or external base URL from its status. Remove any trailing slash, then export the base URL and the exact endpoint used by the first example:

export OPENFUNCTION_BASE_URI="http://function-sample.default.svc.cluster.local"
export OPENFUNCTION_URI="${OPENFUNCTION_BASE_URI}/API7"

If the function endpoint requires HTTP Basic authentication, configure authorization.service_token. The plugin replaces any client Authorization header with the configured credentials. Protect the token in declarative workflows, use HTTPS, and restrict network access to the function endpoint.

Invoke a Synchronous Function​

curl "http://127.0.0.1:9180/apisix/admin/routes/openfunction-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/openfunction",
"plugins": {
"openfunction": {
"function_uri": "${OPENFUNCTION_URI}"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF

The configurations include a conventional fallback backend. The plugin returns the function response before that backend is contacted.

Invoke the exact function endpoint:

curl "http://127.0.0.1:9080/openfunction"

The gateway returns the function response:

Hello, API7!

Preserve a Wildcard Sub-Path​

Update the route to match wildcard sub-paths. The plugin appends the matched suffix to the configured function URI.

Update the route path:

curl "http://127.0.0.1:9180/apisix/admin/routes/openfunction-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"uri":"/openfunction/*"}'

Replace the plugin configuration with the function service base:

curl "http://127.0.0.1:9180/apisix/admin/routes/openfunction-route/plugins/openfunction" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"function_uri": "${OPENFUNCTION_BASE_URI}"
}
EOF

Send a request whose suffix identifies the function path:

curl "http://127.0.0.1:9080/openfunction/API7"

The plugin appends /API7 to the function service base and returns the function response:

Hello, API7!