Welcome to the new Golem Cloud Docs! 👋
Documentation
Import OpenAPI Spec as API Definition

Support to import OpenAPI spec

Until now, we used the native API definition format of Golem. Golem allows you to import API definition in OpenAPI spec format too. This is because, users may have already written an OpenAPI spec for their endpoints for various purposes. By adding extra details of into the same spec, we can use it as an API definition. Internally it gets converted to the Worker Gateway's native format of API definition, discussed in the beginning of this documentation.

The main advantage of this feature is the re-usability of the same endpoint definitions across your system. For example, you can use the same file now to register with Golem's Worker Gateway and register with another external API gateway. More on this below. However once deployed, it returns native format then onwards, and updates needs to be done in the native format.

openapi: 3.0.0
info:
  title: MyOpenAPISpec
  version: 1.0.2
x-golem-api-definition-id: my-shopping-cart-v1
x-golem-api-definition-version: 0.0.7
paths:
  /{user-id}/get-cart-contents:
    get:
      x-golem-worker-bridge:
        worker-name: '"foo"'
        component-id: dba38841-013a-49fa-a1dc-064949832f0c
        component-version: 0
        response: |
          let x = golem:it/api.{checkout}();
          let status: u64 = 200;
          let headers = {ContentType: "json", userid: "foo"};
          let body = "foo";
          {headers: headers, body: body, status: status}
      summary: Get Cart Contents
      description: Get the contents of a user's cart
      parameters:
        - name: user-id
          in: path
          required: true
          schema:
            type: string
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CartItem"
        404:
          description: Contents not found
components:
  schemas:
    CartItem:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        price:
          type: number

To import OpenAPI spec using Golem CLI,

Terminal
 
golem-cli api-definition import open_api.json
 

This will return

Terminal
 
{
  "id": "my-shopping-cart-v1",
  "version": "0.0.7",
  "routes": [
    {
      "method": "Get",
      "path": "/{user-id}/get-cart-contents",
      "binding": {
        "componentId": {
          "componentId": "dba38841-013a-49fa-a1dc-064949832f0c",
          "version": 0
        },
        "workerName": "\"foo\"",
        "idempotencyKey": null,
        "response": "let x = golem:it/api.{checkout}();\nlet status: u64 = 200;\n{headers: {ContentType: \"json\", userid: \"foo\"}, body: \"foo\", status: status}",
        "responseMappingInput": {
          "types": {}
        },
        "workerNameInput": {
          "types": {}
        },
        "idempotencyKeyInput": null
      }
    }
  ],
  "draft": true,
  "createdAt": "2024-10-31T08:56:02.412860043Z"
}