APIG Event Source

API Gateway (APIG) is an API hosting service that helps enterprises to build, manage, and deploy APIs at any scale.

With APIG, your function can be invoked through HTTPS by using a custom REST API and a specified backend.

You can map each API operation (such as, GET and PUT) to a specific function. APIG invokes the relevant function when an HTTPS request is sent to the API backend.

For details, see Using an APIG (Dedicated) trigger.

Example APIG Event

{
  "body": "eyJuYW1lIjogIk9wZW5UZWxla29tQ2xvdWQifQ==",
  "requestContext": {
      "apiId": "bc1dcffd-aa35-474d-897c-d53425a4c08e",
      "requestId": "11cdcdcf33949dc6d722640a13091c77",
      "stage": "RELEASE"
  },
  "queryStringParameters": {
      "responseType": "html"
  },
  "httpMethod": "GET",
  "pathParameters": {},
  "headers": {
      "accept-language": "q=0.5,en-US;q=0.3,en;q=0.2",
      "accept-encoding": "gzip, deflate, br",
      "x-forwarded-port": "443",
      "x-forwarded-for": "103.218.216.98",
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
      "upgrade-insecure-requests": "1",
      "host": "host",
      "x-forwarded-proto": "https",
      "pragma": "no-cache",
      "cache-control": "no-cache",
      "x-real-ip": "103.218.216.98",
      "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
  },
  "path": "/",
  "isBase64Encoded": true
}

Parameter description

Parameter

Type

Description

body

String

Actual request body in string format (Base64 encoded).

requestContext

Map

Request information, including the API gateway configuration, request ID, authentication information, and source.

httpMethod

String

HTTP method

queryStringParameters

Map

Query strings configured in APIG and their actual values

pathParameters

Map

Path parameters configured in APIG and their actual values

headers

Map

Complete headers

path

String

Complete path

isBase64Encoded

Boolean

Default value: true (see Notes below)

Notes

  • When calling a function using APIG, isBase64Encoded is valued true by default, indicating that the request body transferred to FunctionGraph is encoded using Base64 and must be decoded for processing.

  • The function must return an APIGResponse object serialized to a Stream with following structure:

    APIGResponse
    {
      int StatusCode;                      // HTTP status code (e.g., 200, 404, 500)
      string Body;                         // Response body content
      bool IsBase64Encoded;                // Whether the body is base64 encoded
      Dictionary<string, string> Headers;  // HTTP response headers
    }
    

Example

namespace src
{

#if NET6_0_OR_GREATER
  using OpenTelekomCloud.Serverless.Function.Common;
#else
  using HC.Serverless.Function.Common;
  using OpenTelekomCloud.Serverless.Function.Common;
#endif
  using OpenTelekomCloud.Serverless.Function.Events.APIG;
  using System;
  using System.IO;
  using System.Text;

  public class Program
  {

    /// <summary>
    /// Main method - not used in FunctionGraph but needed for compilation
    /// </summary>
    /// <param name="args"></param>
    public static void Main(string[] args)
    {
      Console.WriteLine("This is a FunctionGraph C# runtime program");
    }

    /// <summary>
    /// Handler method for APIG event
    /// </summary>
    public Stream Handler(Stream inputEvent, IFunctionContext context)
    {
      string payload = "";

      JsonSerializer serializer = new JsonSerializer();

      var logger = context.Logger;
      logger.Logf("CSharp runtime test: APIG");

      APIGEvent apigEvent = serializer.Deserialize<APIGEvent>(inputEvent);

      if (apigEvent != null)
      {
        payload = apigEvent.GetBodyAsString();
        logger.Logf("APIG Event Payload: {0}", payload);
        
        // display path parameters
        var parameters = apigEvent.PathParameters.getParameters();
        if (parameters != null)
        {
          logger.Log("################# Path Parameter ##############################################");
          for (int i = 0; i < parameters.Length; i++)
          {
            logger.Logf("Path Parameter[{0}]={1}", i, parameters[i]);
          }
        }
        var headers = apigEvent.Headers;
        if (headers.getAdditionalHeaderKeys() != null)
        {
          logger.Log("################# additional Headers #####################################################");
          foreach (var header in headers.getAdditionalHeaderKeys())
          {
            logger.Logf("Header '{0}' = '{1}'", header, headers.getAdditionalHeader(header)); 
          }
        }
      }
      else
      {
        payload = "?";
      }

      string body = "";

      if (apigEvent.IsBase64Encoded)
      {
        body = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(payload));
      }
      else
      {
        body = payload;
      }

      APIGResponseHeaders responseHeaders = new APIGResponseHeaders();
      responseHeaders.addAdditionalHeader("X-Custom-Header", "CustomValue");

      APIGResponse response = new APIGResponse()
      {
        StatusCode = 200,
        Body = body,
        IsBase64Encoded = apigEvent.IsBase64Encoded,
        Headers = responseHeaders
      };

      return serializer.Serialize<APIGResponse>(response);
    }
  }
}