Appwrite SDK Generator

repository·main·Indexed 43 days ago

https://github.com/appwrite/sdk-generator

A PHP-based tool that automates the creation of multi-language and multi-platform SDKs from OpenAPI specifications (2.0, 3.0, or 3.1) using Twig templates and the Utopia OpenAPI parser.

Tokens
24K
Snippets
80
Records
115
Agent score
55%

What's inside Appwrite SDK Generator

  1. Third-party runtime plugins in Unity SDK

    main

    The Unity SDK includes specific DLLs copied from Microsoft NuGet packages to enable System.Text.Json support on Unity 2021. These dependencies are located in the Assets/Runtime/Core/Plugins/ directory of the Unity project.

    Metadata for these dependencies, including versions and SHA-256 checksums, is maintained in a table within this file and in a structured dependencies.lock.json file for automated audits.

  2. How to name enum values in OpenAPI specs

    main

    To ensure enum values are converted into valid, semantically named identifiers in the generated language (especially for non-Latin text), describe the enum using oneOf branches with a title.

    If a value has no explicit title and cannot form a valid identifier, the generator uses a deterministic positional name like Value1.

    title: ProvinceType
    type: string
    oneOf:
      - title: Capital
        enum: [រាជធានី]
      - title: Province
        enum: [ខេត្ត]
  3. Install the Appwrite SDK Generator

    main

    You can install the SDK Generator using Composer or via Docker. The generator is a PHP library that uses Twig templates and the Utopia OpenAPI parser to generate SDKs from OpenAPI specifications.

    ### CLI
    ```bash
    composer update --ignore-platform-reqs --optimize-autoloader

    Docker (UNIX)

    docker run --rm --interactive --tty --volume "$(pwd)":/app composer install --ignore-platform-reqs

    Docker (Windows)

    docker run --rm --interactive --tty --volume "%cd%":/app composer install --ignore-platform-reqs
  4. Generate specific SDK targets via CLI

    main

    You can use the example.php script to generate specific SDKs by passing arguments for target, platform, and format.

    Usage Pattern: php example.php <target> <platform> <format>

    • <platform>: console, client, or server (defaults to console).
    • <format>: openapi3 or swagger2 (defaults to openapi3).

    Examples:

    php example.php web client
    php example.php node server
    php example.php cli console
    php example.php skills
  5. Generate an SDK from an OpenAPI specification

    main

    To generate a full SDK, you need to:

    1. Parse an OpenAPI document (2.0, 3.0, or 3.1) using Utopia\OpenAPI\Parser.
    2. Create a language instance (e.g., Appwrite\SDK\Language\PHP).
    3. Configure the language instance with composer details if necessary.
    4. Instantiate the Appwrite\SDK\SDK object with the language and spec.
    5. Configure the SDK with platform, version, and license details.
    6. Call generate() with the target directory.
    <?php
    
    require_once 'vendor/autoload.php';
    
    use Appwrite// SDK\SDK;
    use Appwrite\SDK\Language\PHP;
    use Utopia\OpenAPI\Parser;
    
    // Parse an OpenAPI 2, 3.0, or 3.1 document
    $version = '2.0.x';
    $platform = 'server';
    $content = file_get_contents("https://raw.githubusercontent.com/appwrite/specs/main/specs/{$version}/open-api3-{$version}.json");
    $spec = Parser::parse($content);
    
    // Create language instance
    $lang = new PHP();
    $lang->setComposerPackage('my-api')->setComposerVendor('my-company');
    
    // Create the SDK object
    $sdk = new SDK($lang, $spec);
    
    $sdk
        ->setPlatform($platform)
        ->setCoverImage('https://github.com/appwrite/appwrite/raw/main/public/images/github.png')
        ->setLicenseContent('License content here.')
        ->setVersion('v1.1.0');
    
    $sdk->generate(__DIR__ . '/examples/php');
  6. Manage function source: Local vs. GitHub

    main

    When initializing a function, you must choose how the source code is managed via the --source flag:

    • local: The function source is managed locally. You can develop locally using run function and deploy using push function.
    • github: The function is connected to a GitHub repository. This requires a connected GitHub installation in your Appwrite project. You can choose to connect an existing repository or create a new one.
  7. Parse runtime names and versions from runtime strings

    main

    The runtime field in the function configuration uses a hyphen-separated format where the last segment represents the version. You can derive the base runtime name and the version suffix from this string.

    • Runtime Name: The part of the string before the last hyphen. For example, node-22.0 becomes node and python-ml-3.11 becomes python-ml.
    • Runtime Version: The part of the string after the last hyphen. For example, node-22.0 becomes 22.0.
  8. Identify Appwrite Cloud vs Self-Hosted endpoints

    main

    The CLI distinguishes between Appwrite Cloud (including regional endpoints) and self-hosted instances to handle authentication and console URL resolution correctly.

    Cloud Recognition Rules

    • Cloud Hostnames: cloud.appwrite.io and cloud.staging.appwrite.io.
    • Regional Endpoints: Hostnames containing regional prefixes like fra., nyc., syd., sfo., sgp., or tor. (e.g., fra.cloud.appwrite.io) are recognized as regional Cloud endpoints.
    • Cloud Login: An endpoint is considered a Cloud login endpoint if its hostname ends with .appwrite.io (triggering browser-based auth rather than email/password).

    Endpoint Normalization

    • Normalization: Regional Cloud endpoints are collapsed to their base host (e.g., fra.cloud.appwrite.io becomes https://cloud.appwrite.io/v1).
    • Console URL: For Cloud endpoints, the console is served from the base host without the cloud. prefix (e.g., https://appwrite.io).
  9. Configure CLI output formats

    main

    The CLI supports different output modes that affect where diagnostic information and notices are printed:

    • Standard Mode: Notices and warnings are printed to standard output/error.
    • JSON Mode (--json): When using --json, the CLI redirects notices (like update availability) to stderr to ensure that the primary stdout remains a valid, parseable JSON object.
    • Raw Mode (--raw): Similar to JSON mode, notices are redirected to stderr to prevent corrupting the raw data stream on stdout.
  10. How project linking and overwriting works

    main

    The project command manages a local configuration file in your directory.

    • Linking an existing directory: If your directory is already linked to a project, running project without specifying new IDs will prompt you for confirmation before overwriting the existing configuration. This is a destructive action.
    • Creation vs. Linking: If you provide a --project-id, the CLI checks if that project exists. If it exists, it links to it. If it does not exist (404), it creates a new project with that ID.
    • Post-Initialization: If you link to an existing project, you will be asked if you want to pull all resources from that project immediately after linking.