{
    "openapi": "3.1.0",
    "info": {
        "title": "ResellKeys Reseller Automation API",
        "version": "1.0.0",
        "summary": "Automated wholesale Microsoft license key fulfillment & GetCID Phone Activation REST API.",
        "description": "Full HTML & Markdown documentation: https://resellkeys.com/api-docs\n\nUse `rk_test_...` credentials for Sandbox Test Mode ($500 Sandbox Store Credit + 50 Sandbox CID Points, never charges real funds) and `rk_live_...` credentials for Live Production fulfillment."
    },
    "servers": [
        {
            "url": "https://resellkeys.com/api/v1",
            "description": "ResellKeys Production & Sandbox API v1 (mode selected automatically by rk_test_* vs rk_live_* key prefix)"
        }
    ],
    "components": {
        "securitySchemes": {
            "RkApiKeyHeader": {
                "type": "apiKey",
                "in": "header",
                "name": "X-RK-Api-Key",
                "description": "Your `rk_test_...` (Sandbox) or `rk_live_...` (Live) API Key. Pair with either HMAC headers (`X-RK-Timestamp` + `X-RK-Signature`) or `X-RK-Api-Secret`."
            },
            "RkBearerPair": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "rk_key:api_secret",
                "description": "Format: `Authorization: Bearer <API_KEY>:<API_SECRET>` over HTTPS."
            }
        },
        "schemas": {
            "ErrorResponse": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "const": false
                    },
                    "error": {
                        "type": "object",
                        "properties": {
                            "code": {
                                "type": "string"
                            },
                            "message": {
                                "type": "string"
                            }
                        },
                        "required": [
                            "code",
                            "message"
                        ]
                    },
                    "timestamp": {
                        "type": "integer"
                    }
                }
            }
        }
    },
    "security": [
        {
            "RkApiKeyHeader": []
        },
        {
            "RkBearerPair": []
        }
    ],
    "paths": {
        "/account": {
            "get": {
                "operationId": "getAccountBalances",
                "summary": "Get Store Credit & CID Points balances",
                "responses": {
                    "200": {
                        "description": "Authenticated account balances and mode (`test` or `live`)."
                    },
                    "401": {
                        "description": "Invalid API credentials or signature."
                    }
                }
            }
        },
        "/account/transactions": {
            "get": {
                "operationId": "listAccountTransactions",
                "summary": "List paginated Store Credit wallet ledger entries",
                "parameters": [
                    {
                        "name": "page",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "default": 1
                        }
                    },
                    {
                        "name": "per_page",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "default": 20,
                            "maximum": 100
                        }
                    },
                    {
                        "name": "order_id",
                        "in": "query",
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Paginated Store Credit transactions (`debit` and `credit`)."
                    }
                }
            }
        },
        "/products": {
            "get": {
                "operationId": "listProducts",
                "summary": "List active catalog products, USD pricing, instant_delivery status, limits, and warranty options",
                "parameters": [
                    {
                        "name": "q",
                        "in": "query",
                        "description": "Optional search keyword filter",
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "List of active API-eligible products."
                    }
                }
            }
        },
        "/products/{productId}": {
            "get": {
                "operationId": "getProduct",
                "summary": "Get single product availability, instant_delivery flag, pricing, and warranty metadata",
                "parameters": [
                    {
                        "name": "productId",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Single product metadata."
                    },
                    "404": {
                        "description": "Product not found or inactive."
                    }
                }
            }
        },
        "/orders": {
            "post": {
                "operationId": "createOrder",
                "summary": "Purchase digital license keys using Store Credit (idempotent via client_reference_id)",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "product_id": {
                                        "type": "integer",
                                        "description": "Product ID for single-item order"
                                    },
                                    "quantity": {
                                        "type": "integer",
                                        "default": 1
                                    },
                                    "extended_warranty": {
                                        "type": "boolean",
                                        "default": false
                                    },
                                    "items": {
                                        "type": "array",
                                        "description": "Optional multi-item array (max 25 line items, 500 total keys)",
                                        "items": {
                                            "type": "object",
                                            "properties": {
                                                "product_id": {
                                                    "type": "integer"
                                                },
                                                "quantity": {
                                                    "type": "integer"
                                                },
                                                "extended_warranty": {
                                                    "type": "boolean"
                                                }
                                            },
                                            "required": [
                                                "product_id",
                                                "quantity"
                                            ]
                                        }
                                    },
                                    "client_reference_id": {
                                        "type": "string",
                                        "description": "Your store order ID for safe idempotent retries"
                                    },
                                    "instant_only": {
                                        "type": "boolean",
                                        "default": false,
                                        "description": "If true, returns 409 insufficient_instant_stock ($0 charged) when vault stock is short rather than queuing as processing"
                                    },
                                    "webhook_url": {
                                        "type": "string",
                                        "format": "uri"
                                    },
                                    "simulate_status": {
                                        "type": "string",
                                        "enum": [
                                            "complete",
                                            "processing",
                                            "canceled",
                                            "refunded"
                                        ],
                                        "description": "Sandbox (`rk_test_*`) only: simulate async processing or cancellation/refund webhook lifecycle"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Order fulfilled (`complete`) with keys immediately, or queued (`processing`) for webhook callback."
                    },
                    "402": {
                        "description": "Insufficient Store Credit (`insufficient_store_credit`)."
                    },
                    "409": {
                        "description": "Product out of stock or `instant_only` vault stock insufficient (`insufficient_instant_stock`)."
                    },
                    "422": {
                        "description": "Validation or purchase limit error."
                    }
                }
            },
            "get": {
                "operationId": "listOrders",
                "summary": "List API orders or look up an order by client_reference_id",
                "parameters": [
                    {
                        "name": "client_reference_id",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "status",
                        "in": "query",
                        "schema": {
                            "type": "string",
                            "enum": [
                                "complete",
                                "processing",
                                "canceled",
                                "refunded",
                                "failed"
                            ]
                        }
                    },
                    {
                        "name": "page",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "default": 1
                        }
                    },
                    {
                        "name": "per_page",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "default": 20,
                            "maximum": 100
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Paginated order list including delivered keys."
                    }
                }
            }
        },
        "/orders/{orderId}": {
            "get": {
                "operationId": "getOrder",
                "summary": "Retrieve single order status, items, and delivered license keys",
                "parameters": [
                    {
                        "name": "orderId",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Order details and keys."
                    },
                    "404": {
                        "description": "Order not found."
                    }
                }
            }
        },
        "/orders/{orderId}/replay-webhook": {
            "post": {
                "operationId": "replayOrderWebhook",
                "summary": "Replay signed webhook for an existing Sandbox or Live order",
                "parameters": [
                    {
                        "name": "orderId",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "requestBody": {
                    "required": false,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "event": {
                                        "type": "string",
                                        "enum": [
                                            "order.completed",
                                            "order.canceled",
                                            "order.refunded"
                                        ]
                                    },
                                    "webhook_url": {
                                        "type": "string",
                                        "format": "uri"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Signed webhook dispatched successfully."
                    }
                }
            }
        },
        "/webhooks/test": {
            "post": {
                "operationId": "sendTestWebhook",
                "summary": "Send a signed test order.completed webhook ping to verify your receiver",
                "requestBody": {
                    "required": false,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "webhook_url": {
                                        "type": "string",
                                        "format": "uri"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Webhook endpoint returned HTTP 2xx."
                    },
                    "502": {
                        "description": "Target webhook endpoint returned non-2xx or timed out."
                    }
                }
            }
        },
        "/cid/generate": {
            "post": {
                "operationId": "generateCid",
                "summary": "Generate Microsoft Phone Activation Confirmation ID (CID) from 54/63-digit Installation ID (1 CID Point)",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "iid"
                                ],
                                "properties": {
                                    "iid": {
                                        "type": "string",
                                        "description": "54-digit or 63-digit Microsoft Installation ID (dashes/spaces allowed)"
                                    },
                                    "product_key": {
                                        "type": "string",
                                        "description": "Optional purchased phone-activation product key"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Confirmation ID (`cid`) and blocks (`A`\u2013`H`)."
                    },
                    "402": {
                        "description": "Insufficient CID Points (`insufficient_cid_points`)."
                    },
                    "422": {
                        "description": "Invalid Installation ID or Microsoft phone verification error."
                    }
                }
            }
        },
        "/cid/history": {
            "get": {
                "operationId": "listCidHistory",
                "summary": "List paginated CID Points generation log",
                "parameters": [
                    {
                        "name": "page",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "default": 1
                        }
                    },
                    {
                        "name": "per_page",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "default": 20,
                            "maximum": 100
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Paginated CID generation history."
                    }
                }
            }
        }
    }
}