{
  "openapi": "3.0.3",
  "info": {
    "title": "Fawry Connect API",
    "version": "1.0.0-sandbox",
    "description": "Fawry Financial open banking API (AIS & PIS). Sandbox uses simulated data only. Production access subject to SAMA authorization.",
    "contact": {
      "email": "developers@fawryfinancial.com"
    }
  },
  "servers": [
    {
      "url": "https://sandbox.api.fawryfinancial.com",
      "description": "Sandbox"
    },
    {
      "url": "https://api.fawryfinancial.com",
      "description": "Production (after SAMA authorization)"
    }
  ],
  "paths": {
    "/v1/oauth/token": {
      "post": {
        "summary": "Obtain access token (client credentials, mTLS)",
        "tags": [
          "Auth"
        ],
        "responses": {
          "200": {
            "description": "Token"
          }
        }
      }
    },
    "/v1/consents": {
      "post": {
        "summary": "Create consent",
        "tags": [
          "Consents"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "201": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ConsentRequest"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/consents/{consent_id}": {
      "get": {
        "summary": "Get consent",
        "tags": [
          "Consents"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "parameters": [
          {
            "name": "consent_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      },
      "delete": {
        "summary": "Revoke consent",
        "tags": [
          "Consents"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "204": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "parameters": [
          {
            "name": "consent_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/accounts": {
      "get": {
        "summary": "List accounts",
        "tags": [
          "AIS"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/accounts/{account_id}/balances": {
      "get": {
        "summary": "Get balances",
        "tags": [
          "AIS"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "parameters": [
          {
            "name": "account_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/accounts/{account_id}/transactions": {
      "get": {
        "summary": "Get transactions",
        "tags": [
          "AIS"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "parameters": [
          {
            "name": "account_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "from",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          }
        ]
      }
    },
    "/v1/payments": {
      "post": {
        "summary": "Initiate payment",
        "tags": [
          "PIS"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "201": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PaymentRequest"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/payments/{payment_id}": {
      "get": {
        "summary": "Get payment status",
        "tags": [
          "PIS"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "parameters": [
          {
            "name": "payment_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/insights/affordability": {
      "post": {
        "summary": "Affordability insights",
        "tags": [
          "Intelligence"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/InsightRequest"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/webhooks": {
      "post": {
        "summary": "Register webhook",
        "tags": [
          "Webhooks"
        ],
        "security": [
          {
            "oauth2": []
          }
        ],
        "responses": {
          "201": {
            "description": "Success"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WebhookRequest"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "oauth2": {
        "type": "oauth2",
        "flows": {
          "clientCredentials": {
            "tokenUrl": "/v1/oauth/token",
            "scopes": {}
          },
          "authorizationCode": {
            "authorizationUrl": "/authorize",
            "tokenUrl": "/v1/oauth/token",
            "scopes": {
              "accounts": "Accounts",
              "payments": "Payments"
            }
          }
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "request_id": {
            "type": "string"
          }
        }
      },
      "ConsentRequest": {
        "type": "object",
        "required": [
          "customer_ref",
          "permissions",
          "purpose",
          "expires_in_days",
          "redirect_uri"
        ],
        "properties": {
          "customer_ref": {
            "type": "string"
          },
          "permissions": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "accounts",
                "balances",
                "transactions",
                "account_holder"
              ]
            }
          },
          "purpose": {
            "type": "string"
          },
          "expires_in_days": {
            "type": "integer",
            "maximum": 365
          },
          "redirect_uri": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "PaymentRequest": {
        "type": "object",
        "required": [
          "amount",
          "currency",
          "creditor_iban",
          "reference"
        ],
        "properties": {
          "amount": {
            "type": "number"
          },
          "currency": {
            "type": "string",
            "enum": [
              "SAR"
            ]
          },
          "creditor_iban": {
            "type": "string"
          },
          "creditor_name": {
            "type": "string"
          },
          "reference": {
            "type": "string"
          },
          "execution_date": {
            "type": "string",
            "format": "date"
          }
        }
      },
      "InsightRequest": {
        "type": "object",
        "required": [
          "consent_id"
        ],
        "properties": {
          "consent_id": {
            "type": "string"
          },
          "months": {
            "type": "integer",
            "default": 6
          }
        }
      },
      "WebhookRequest": {
        "type": "object",
        "required": [
          "url",
          "events"
        ],
        "properties": {
          "url": {
            "type": "string",
            "format": "uri"
          },
          "events": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "consent.authorised",
                "consent.revoked",
                "consent.expired",
                "payment.completed",
                "payment.rejected"
              ]
            }
          }
        }
      }
    }
  }
}