All Products
Search
Document Center

Function Compute:Web functions

Last Updated:Aug 23, 2026

This topic describes the invocation methods, limitations, and code examples for functions in a custom runtime.

Background information

A custom runtime can host your HTTP server. Function Compute converts function invocation requests into HTTP requests and sends them to your HTTP server. Function Compute then converts the responses from your HTTP server into function invocation responses and returns them to the client. The following figure shows the process.

You can invoke a function in one of the following two ways:

  • HTTP call (recommended): Invoke the function over HTTP, for example, by using an HTTP trigger or a custom domain name.

  • API call: Invoke the function by calling the InvokeFunction API, for example, by using an SDK to invoke the function or by triggering the function through an event source.

Different invocation methods result in different request and response formats of your HTTP server.

image

Limits

  • You can create only one HTTP trigger for each version or alias of a function. For more information, see Manage versions and Manage aliases.

  • HTTP request limits

    • Request headers do not support custom fields that start with x-fc- or the following custom fields:

      • connection

      • keep-alive

    • If a request exceeds the following limits, a 400 status code and an InvalidArgument error code are returned.

      • Header size: The total size of all keys and values in the headers cannot exceed 8 KB.

      • Path size: The total size of the path, including all query parameters, cannot exceed 4 KB.

      • Body size: The total size of the request body for a synchronous invocation cannot exceed 32 MB. The total size of the request body for an asynchronous invocation cannot exceed 128 KB.

  • HTTP response limits

    • Response headers do not support custom fields that start with x-fc- or the following custom fields:

      • connection

      • content-length

      • date

      • keep-alive

      • server

      • content-disposition:attachment

        Note

        For security reasons, when you use the default aliyuncs.com domain of Function Compute, the server forcibly adds the content-disposition: attachment header to the response headers. This header causes the response to be downloaded as an attachment in the browser. To remove this restriction, configure a custom domain name.

    • If a response exceeds the following limits, a 502 status code and a BadResponse error code are returned.

      • Header size: The total size of all keys and values in the headers cannot exceed 8 KB.

  • Other usage notes

    You can map different HTTP access paths to your function by binding a custom domain name. For more information, see Configure a custom domain name.

HTTP call (recommended)

For HTTP invocations, Function Compute uses the passthrough mode. It passes the HTTP request of the client through to your HTTP server and passes the response of the HTTP server through to the client. Some system-reserved fields are not passed through. For more information, see Limits.

Request headers

When you invoke a function by using an HTTP trigger or a custom domain name, Function Compute allows you to configure request headers to control the request behavior. The following table describes the supported request headers.

Name

Type

Required

Example

Description

X-Fc-Invocation-Type

String

No

Sync

The invocation method. For more information, see Invocation methods. Valid values:

  • Sync: synchronous invocation.

  • Async: asynchronous invocation.

X-Fc-Log-Type

String

No

Tail

The logs to return in the response. Valid values:

  • Tail: returns the last 4 KB of logs generated by the current request.

  • None: does not return the request logs. This is the default value.

Response headers

When you invoke a function by using an HTTP trigger or a custom domain name, the response contains some response headers that Function Compute adds by default. The following table describes the response headers.

Name

Description

Example

X-Fc-Request-Id

The request ID of the function invocation.

dab25e58-9356-4e3f-97d6-f044c4****

API call

For invocations by using the InvokeFunction API, Function Compute converts the InvokeFunction request into an HTTP request and sends it to your HTTP server. The conversion rules are as follows:

  • The event parameter of InvokeFunction is converted into the message body of the HTTP request.

  • path is /invoke.

  • method is POST.

  • The Content-Type message header is application/octet-stream.

Function Compute converts the response of your HTTP server into the InvokeFunction response and returns it to the client. The conversion rules are as follows:

  • The HTTP response body is converted into the InvokeFunction response body.

  • The HTTP response headers and status code are lost during the conversion.

Invoke API request conversion example

Invoke request

HTTP request (the request received by the HTTP server)

Invoke API request content:

"hello world"
> POST /invoke HTTP/1.1
> Host: 21.0.X.X
> Content-Length: 11
> Content-Type: application/octet-stream

hello world

Invoke API response output example

HTTP response

Invoke response

< HTTP/1.1 200 OK
< Date: Mon, 10 Jul 2025 10:37:15 GMT
< Content-Type: application/octet-stream
< Content-Length: 11
< Connection: keep-alive

hello world
hello world
< HTTP/1.1 400 Bad Request
< Date: Mon, 10 Jul 2025 10:37:15 GMT
< Content-Type: application/octet-stream
< Content-Length: 28
< Connection: keep-alive

{"errorMessage":"exception"}
{"errorMessage":"exception"}

Function Compute response codes and response headers

A custom runtime is essentially an HTTP server that you implement. Therefore, each function invocation is an HTTP request, and each response includes a response code and response headers.

  • Response code StatusCode

    • 200: success.

    • 404: failure.

  • Response header x-fc-status

    • 200: success.

    • 404: failure.

You can use the x-fc-status response header to report to Function Compute whether the function was executed successfully.

  • If you do not set x-fc-status: Function Compute assumes that the invocation was executed successfully by default. However, your function may have encountered an exception that was not reported to Function Compute. Function Compute considers the execution error-free. This may not affect your business logic, but it affects monitoring and observability. The following code shows an example:

        print("FC Invoke Start RequestId: " + rid)
        data = request.stream.read()
        print("Path: " + path)
        print("Data: " + str(data))
        # Simulate an exception to trigger a runtime error
        raise Exception("mock exception")
        print("FC Invoke End RequestId: " + rid)
        return "Hello, World!"
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=9000)
  • If you set x-fc-status: If your function encounters an exception, you can use the x-fc-status response header to report the execution failure to Function Compute, and the error stack information is printed to the logs. As shown in the following example, after you set the x-fc-status response header to 404, Function Compute identifies the invocation as a failed execution. The error type is InvocationError, and the returned result is mock exception. The sample code in app.py is as follows:

    @app.route('/', defaults={'path': ''})
    @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
    def hello_world(path):
        rid = request.headers.get(REQUEST_ID_HEADER)
        print("FC Invoke Start RequestId: " + rid)
        try:
            raise Exception("mock exception")
        except Exception as e:
            print("FC Invoke End RequestId: " + rid + ", Error: Unhandled Exception")
            print(str(e))
            return str(e), 404, [{"x-fc-status", "404"}]
Note

In the returned HTTP response, we recommend that you set both StatusCode and x-fc-status.

Code example

If a trigger is configured for the function, you can implement an HTTP server in any language. This topic uses Python as an example. The sample code is as follows.

Note

The sample code depends on a Python environment and the Flask library. We recommend that you select Web Function for the function creation method and Python 3.10 for the runtime.

import os
from flask import Flask
from flask import request
REQUEST_ID_HEADER = 'x-fc-request-id'
app = Flask(__name__)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def hello_world(path):
    rid = request.headers.get(REQUEST_ID_HEADER)
    data = request.stream.read()
    print("Path: " + path)
    print("Data: " + str(data))
    return "Hello, World!", 200, [('Function-Name', os.getenv('FC_FUNCTION_NAME'))]
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000)

The sample code is explained as follows:

  • @app.route('/', defaults={'path': ''}): the default route, which corresponds to the root path.

  • @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE']): a dynamic route with a path parameter. It can handle GET, POST, PUT, and DELETE requests. The value of the path parameter is passed to the hello_world function as the path argument.

  • rid = request.headers.get(REQUEST_ID_HEADER): obtains the value of the x-fc-request-id field in the request headers.

  • data = request.stream.read(): reads the request content and assigns it to the data variable.

  • return "Hello, World!", 200, [('Function-Name', os.getenv('FC_FUNCTION_NAME'))]: returns a response body that contains "Hello, World!", sets the status code to 200, and includes a Function-Name header.