# Geksy: onboarding steps for programs and AI agents (Arazzo 1.0, https://spec.openapis.org/arazzo/latest.html).
# Operations are described in https://geksy.cc/openapi.json; every error has `action` (what to do next) and `doc_url`.
arazzo: 1.0.1
info:
  title: Geksy onboarding for programs and AI agents
  version: 1.0.0
  description: >-
    Ready-made call sequences for the Geksy cloud API: learn plans and error codes, check a workspace,
    pay for a plan from the workspace balance, create a profile with your own proxy or a Geksy proxy.
    Authentication: a workspace API key (`ak_…`) issued in the web app; pass it as `Authorization: Bearer <key>`.
    Launching a browser profile is done by the Geksy app on a computer (local API, CDP) and is not part of the
    cloud API. Workflows that are planned but not available yet are listed under `x-planned-workflows`.
sourceDescriptions:
  - name: geksy
    url: https://geksy.cc/openapi.json
    type: openapi

workflows:
  - workflowId: discover
    summary: Plans, prices and the error-code catalog (no authentication)
    steps:
      - stepId: plans
        operationId: publicPlans
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          plans: $response.body
      - stepId: errors
        operationId: listErrorCodes
        parameters:
          - name: lang
            in: query
            value: en
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          errors: $response.body#/errors
          actionKinds: $response.body#/action_kinds
    outputs:
      plans: $steps.plans.outputs.plans
      errors: $steps.errors.outputs.errors

  - workflowId: signInByCode
    summary: Get an API key without a password — a person approves a short code (OAuth device flow, RFC 8628)
    description: >-
      Show `user_code` and `verification_uri_complete` to a person. They choose the workspace, permissions, key
      lifetime and spend limit in the web app. Poll `token` every `interval` seconds: `authorization_pending` — keep
      waiting, `slow_down` — add 5 s, `access_denied` / `expired_token` — stop. Bodies may be form-encoded or JSON.
    inputs:
      type: object
      required: [client_name]
      properties:
        client_name: {type: string, description: "program name the person will see"}
        scope: {type: string, description: "space-separated permissions, e.g. 'api.use profile.launch'"}
    steps:
      - stepId: start
        operationId: deviceAuthorization
        requestBody:
          contentType: application/x-www-form-urlencoded
          payload:
            client_id: $inputs.client_name
            scope: $inputs.scope
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          deviceCode: $response.body#/device_code
          userCode: $response.body#/user_code
          verificationUri: $response.body#/verification_uri_complete
          interval: $response.body#/interval
      - stepId: poll
        operationId: token
        requestBody:
          contentType: application/x-www-form-urlencoded
          payload:
            grant_type: urn:ietf:params:oauth:grant-type:device_code
            device_code: $steps.start.outputs.deviceCode
        successCriteria:
          - condition: $statusCode == 200
        onFailure:
          - name: waitForPerson
            type: retry
            retryAfter: 5
            retryLimit: 180
            criteria:
              - condition: $response.body#/error == 'authorization_pending' || $response.body#/error == 'slow_down'
        outputs:
          apiKey: $response.body#/access_token
          workspaceId: $response.body#/workspace_id
    outputs:
      apiKey: $steps.poll.outputs.apiKey
      workspaceId: $steps.poll.outputs.workspaceId

  - workflowId: whoAmI
    summary: With only an API key — find its workspace, effective permissions, plan, rate and spend limits
    inputs:
      type: object
      required: [api_key]
      properties:
        api_key: {type: string, description: "workspace API key ak_…"}
    steps:
      - stepId: key
        operationId: keySelf
        parameters:
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          workspaceId: $response.body#/workspace_id
          permissions: $response.body#/permissions
          spend: $response.body#/spend
      - stepId: usage
        operationId: usage
        parameters:
          - {name: ws_id, in: path, value: $steps.key.outputs.workspaceId}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          proxy: $response.body#/proxy
          money: $response.body#/money
    outputs:
      workspaceId: $steps.key.outputs.workspaceId
      permissions: $steps.key.outputs.permissions

  - workflowId: childKeyForSubtask
    summary: Issue a short-lived child key with narrower permissions and a spend limit for a subtask
    description: >-
      Needs the keys.manage permission. The child key is never wider than the parent (permissions, expiry, spend
      limit) and is revoked together with it. Rotate a key without downtime with `rotateSelf`.
    inputs:
      type: object
      required: [workspace_id, api_key, scopes]
      properties:
        workspace_id: {type: string, format: uuid}
        api_key: {type: string}
        scopes: {type: array, items: {type: string}, description: "e.g. [api.use, profile.launch]"}
        minutes: {type: integer, description: "lifetime, 5…525600"}
        spend_limit_day_micro: {type: integer, description: "1 USD = 1000000"}
    steps:
      - stepId: create
        operationId: createKey
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        requestBody:
          contentType: application/json
          payload:
            name: subtask
            scopes: $inputs.scopes
            expires_in_minutes: $inputs.minutes
            spend_limit_day_micro: $inputs.spend_limit_day_micro
        successCriteria:
          - condition: $statusCode == 201
        outputs:
          childKey: $response.body#/key
          childId: $response.body#/id
    outputs:
      childKey: $steps.create.outputs.childKey

  - workflowId: checkWorkspace
    summary: What the key can see — workspace, plan limits and usage, balance and next charge
    inputs:
      type: object
      required: [workspace_id, api_key]
      properties:
        workspace_id: {type: string, format: uuid}
        api_key: {type: string, description: "workspace API key ak_…"}
    steps:
      - stepId: workspace
        operationId: getWorkspace
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
      - stepId: usage
        operationId: usage
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          limits: $response.body#/limits
          usage: $response.body#/usage
      - stepId: billing
        description: Needs the billing.manage permission; skip on 403.
        operationId: getBilling
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          nextCharge: $response.body#/next_charge
    outputs:
      limits: $steps.usage.outputs.limits
      usage: $steps.usage.outputs.usage

  - workflowId: buyPlan
    summary: Pay for a plan
    description: >-
      `quote` tells the price. A free total completes at once (no redirect_url); otherwise the response carries
      payment_link / redirect_url — a payment page for a person (with `provider: balance` the plan is paid from the balance at once).
      Over the key's spend limit: `402 spend_limit_reached` with
      `params.approval_id`; after a person approves, repeat checkout with the `X-Spend-Approval` header.
    inputs:
      type: object
      required: [workspace_id, api_key, plan_code, period]
      properties:
        workspace_id: {type: string, format: uuid}
        api_key: {type: string}
        plan_code: {type: string, description: "from publicPlans, e.g. start"}
        period: {type: string, enum: [month, year]}
        idempotency_key: {type: string, description: "any unique string; repeat it when retrying"}
    steps:
      - stepId: quote
        operationId: billingQuote
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: plan_code, in: query, value: $inputs.plan_code}
          - {name: period, in: query, value: $inputs.period}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          total: $response.body#/total_minor
      - stepId: checkout
        operationId: checkout
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
          - {name: Idempotency-Key, in: header, value: $inputs.idempotency_key}
        requestBody:
          contentType: application/json
          payload:
            plan_code: $inputs.plan_code
            period: $inputs.period
        successCriteria:
          - condition: $statusCode == 200 || $statusCode == 201
        outputs:
          paymentId: $response.body#/payment_id
          redirectUrl: $response.body#/redirect_url
      - stepId: payment
        operationId: billingGetPayment
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: payment_id, in: path, value: $steps.checkout.outputs.paymentId}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          status: $response.body#/status
    outputs:
      paymentStatus: $steps.payment.outputs.status
      redirectUrl: $steps.checkout.outputs.redirectUrl

  - workflowId: profileWithOwnProxy
    summary: Import your proxy and create a profile that uses it
    inputs:
      type: object
      required: [workspace_id, api_key, proxy, name]
      properties:
        workspace_id: {type: string, format: uuid}
        api_key: {type: string}
        proxy: {type: string, description: "host:port:user:pass, user:pass@host:port or scheme://…"}
        name: {type: string}
    steps:
      - stepId: importProxy
        operationId: importProxies
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        requestBody:
          contentType: application/json
          payload:
            text: $inputs.proxy
            check: true
        successCriteria:
          - condition: $statusCode == 200 || $statusCode == 201
        outputs:
          proxyId: $response.body#/created/0/id
      - stepId: createProfile
        operationId: createProfile
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        requestBody:
          contentType: application/json
          payload:
            name: $inputs.name
            proxy_id: $steps.importProxy.outputs.proxyId
        successCriteria:
          - condition: $statusCode == 201
        outputs:
          profileId: $response.body#/id
    outputs:
      profileId: $steps.createProfile.outputs.profileId

  - workflowId: profileWithGeksyProxy
    summary: Buy a Geksy proxy from the balance and attach it to a profile
    description: >-
      The balance is topped up with a payment (operation `proxyShopTopup`, payment_link for a person). Products and offers come from
      `catalog`. `402 proxy_balance_low` carries `action` with the call that tops the wallet up.
    inputs:
      type: object
      required: [workspace_id, api_key, product_code, profile_id]
      properties:
        workspace_id: {type: string, format: uuid}
        api_key: {type: string}
        product_code: {type: string, description: "from catalog"}
        country: {type: string, description: "ISO 3166-1 alpha-2, e.g. DE"}
        profile_id: {type: string, format: uuid}
        wallet_topup_minor: {type: integer, description: "cents to top the balance up with"}
    steps:
      - stepId: catalog
        operationId: catalog
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        successCriteria:
          - condition: $statusCode == 200
      - stepId: fillWallet
        operationId: proxyShopTopup
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        requestBody:
          contentType: application/json
          payload:
            amount_minor: $inputs.wallet_topup_minor
        successCriteria:
          - condition: $statusCode == 200
        outputs:
          paymentLink: $response.body#/payment_link
      - stepId: buy
        operationId: buy
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        requestBody:
          contentType: application/json
          payload:
            product_code: $inputs.product_code
            country: $inputs.country
            count: 1
        successCriteria:
          - condition: $statusCode == 201
        outputs:
          proxyId: $response.body#/0/proxy_id
      - stepId: attach
        operationId: patchProfile
        parameters:
          - {name: ws_id, in: path, value: $inputs.workspace_id}
          - {name: profile_id, in: path, value: $inputs.profile_id}
          - {name: Authorization, in: header, value: "Bearer {$inputs.api_key}"}
        requestBody:
          contentType: application/json
          payload:
            proxy_id: $steps.buy.outputs.proxyId
        successCriteria:
          - condition: $statusCode == 200
    outputs:
      proxyId: $steps.buy.outputs.proxyId

# Planned (not available yet) — steps will be added when the operations ship.
x-planned-workflows:
  - workflowId: agentSelfSignup
    summary: An agent creates its own sandbox workspace and API key without a person; a person may claim it later
  - workflowId: cryptoTopupDirect
    summary: Top up the balance by API with a crypto address and amount, no payment page
  - workflowId: registerRunnerDevice
    summary: Register a headless Linux/Docker runner as a device with an API key, then launch profiles on it
