Tyk API Gateway

repository·master·Indexed 25 days ago

https://github.com/tyktechnologies/tyk

A cloud-native, high-performance API Gateway for securing, managing, and transforming REST, GraphQL, TCP, and gRPC traffic. Documentation covers OpenAPI Specification (OAS) schema validation, the Coprocess framework for custom middleware in Python, Lua, and gRPC, and the implementation of language bindings.

Tokens
21.6K
Snippets
59
Records
126
Agent score
92%

What's inside Tyk

  1. Overview of the OAS Package

    master

    The OAS package provides support for OpenAPI Specification (OAS) schema validation and integration with Tyk's custom extensions. It handles:

    • Loading and validating OAS schemas (3.0, 3.1, and future versions).
    • Injecting Tyk-specific extensions (x-tyk-api-gateway) into OAS schemas.
    • Validating OAS documents and templates against schemas.
    • Managing schema versions and defaults.
  2. Overview of Tyk API Gateway Features

    master

    Tyk Gateway is a cloud-native, open-source API Gateway supporting REST, GraphQL, TCP, and gRPC protocols. Key features include:

    • Authentication: Supports OIDC, JWT, Bearer Tokens, Basic Auth, and Client Certificates.
    • Traffic Management: Rate Limiting, Quotas, and IP AllowListing.
    • Content Mediation: Transform request/response headers and convert between protocols (e.g., SOAP to GraphQL).
    • Extensibility: Plugin architecture supporting Python, JavaScript, Go, or any language via gRPC.
    • API Management: OpenAPI (Swagger) import, API Versioning, and Granular Access Control.
    • Security: CORS, Blocklists/Allowlists/Ignore lists, and Webhooks for events like Quota Violations.
    • Operations: Hitless reloads (dynamic configuration updates without affecting active requests) and Analytics logging.
  3. Use the Tyk testing framework

    master

    The Tyk testing framework is designed to run tests through the full HTTP stack, mimicking real user behavior. It separates test definition logic from the test runner and provides official mocks for the Dashboard, RPC, and Bundler. Most testing scenarios require a gateway instance to access gateway functions and properties.

    The framework is located in the github.com/TykTechnologies/tyk/test package.

  4. Quick Start: Install Tyk Gateway using Docker Compose

    master

    The fastest way to get started with Tyk Gateway is using the tyk-gateway-docker repository. This method installs Tyk Gateway and Redis automatically.

    1. Clone the deployment repository:
      git clone https://github.com/TykTechnologies/tyk-gateway-docker
    2. Navigate to the directory:
      cd tyk-gateway-docker
    3. Deploy the services:
      docker-compose up
      Note: Use docker-compose up -d to run in detached mode.

    To verify the installation, check the 'hello' endpoint:

    curl localhost:8080/hello

    Expected output:

    {"status": "pass", "version": "v3.2.1", "description": "Tyk GW"}
    git clone https://github.com/TykTechnologies/tyk-gateway-docker
    cd tyk-gateway-docker
    docker-compose up
    curl localhost:8080/hello
  5. Generate the Tyk Gateway Swagger.yml file

    master

    To generate the swagger.yml file containing all gateway endpoints, follow these steps:

    1. Install Redocly on your system.
    2. Clone the gateway repository and switch to the generate-swagger branch.
    3. Navigate to the swagger/cmd directory.
    4. Execute the make generate command.

    The make generate command performs the following sequence:

    • Removes any existing swagger.yml.
    • Runs the Go generation logic via go run main.go.
    • Lints the resulting file using redocly lint swagger.yml.
  6. Compile Tyk Gateway from source

    master

    To build the Tyk Gateway from source, clone the repository and use the Go build command.

    Requirements:

    • Go version: 1.22 is required to build the master development branch.
    • Supported Platforms: Linux/amd64, Linux/i386, and Linux/arm64.

    Note: This process builds the gateway binary from the current source code.

    git clone https://github.com/TykTechnologies/tyk
    go build
  7. Build Tyk Gateway with Python Coprocess support

    master

    To use Python middleware and event handlers, you must build the Tyk Gateway using specific build tags. This ensures the Go compiler includes the necessary C bindings for the Python interpreter.

    Run the following command from the repository root:

    go build -tags 'coprocess python'
  8. Handle Required and Optional Fields in OAS Structs

    master

    Follow these rules for field definitions to maintain data integrity and documentation accuracy:

    Required Fields

    • Do not use omitempty in struct tags.
    • Do not use pointer types.
    • Append the comment // required to the end of the field line for automated documentation.
    • Convention: Add a Go doc comment linking to the classic API definition using the format: // Tyk classic API definition: !use_key_name``.

    Optional Fields

    • Use the omitempty tag.
    • Use pointer types for structs.
    • Ensure omitempty is added to slice fields that are optional.
  9. Enable Coprocess (gRPC) in Tyk

    master

    To use Tyk middleware via a gRPC backend, you must enable the Coprocess feature and specify your gRPC server address in the tyk.conf file. The coprocess_grpc_server setting supports both UNIX sockets and TCP connections (e.g., tcp://127.0.0.1:5555).

    {
      "coprocess_options": {
        "enable_coprocess": true,
        "coprocess_grpc_server": "tcp://127.0.0.1:5555"
      }
    }
  10. Run MCP list filter integration tests

    master

    Integration tests for MCP list filtering require the gateway, dashboard, and a mock MCP server to be running. These tests are executed using pytest from the analytics test directory.

    # Requires running gateway + dashboard + mock MCP server
    cd tyk-analytics/tests/api
    pytest tests/mcp/mcp_list_filter_test.py -v -m mcp