{
  "openapi": "3.1.0",
  "info": {
    "title": "Datafor API",
    "version": "1.0.0",
    "description": "Run ready-made scrapers (Google SERP, Keyword Planner, Amazon.com.tr, Trendyol, URL scraper) through one endpoint. Billing is per run with credits; failed runs are refunded automatically.",
    "contact": {
      "name": "Datafor",
      "email": "hello@datafor.dev",
      "url": "https://datafor.dev/contact"
    },
    "termsOfService": "https://datafor.dev/terms"
  },
  "externalDocs": {
    "description": "Docs",
    "url": "https://datafor.dev/docs"
  },
  "servers": [
    {
      "url": "https://api.datafor.dev"
    }
  ],
  "security": [
    {
      "apiKey": []
    }
  ],
  "paths": {
    "/v1/run": {
      "post": {
        "operationId": "runScript",
        "summary": "Run a scraper",
        "description": "Charges the script’s credit cost, runs it on a worker and returns the result. Worker errors and timeouts are refunded.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "oneOf": [
                  {
                    "title": "google_keyword_suggestor",
                    "description": "Collect Google autocomplete keyword suggestions with configurable country, language, and depth. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "google_keyword_suggestor"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "query": {
                            "type": "string",
                            "description": "Seed keyword or phrase to expand (e.g. \"sofa set\")."
                          },
                          "country": {
                            "type": "string",
                            "description": "ISO-2 country code for geographic location (gl). Defaults to \"tr\".",
                            "default": "tr"
                          },
                          "language": {
                            "type": "string",
                            "description": "ISO-2 language code for suggestions (hl). Defaults to \"tr\".",
                            "default": "tr"
                          },
                          "depth": {
                            "type": "integer",
                            "description": "Expansion layers (1–2). Layer 2 also fetches suggestions for layer-1 results.",
                            "default": 1,
                            "minimum": 1,
                            "maximum": 2
                          }
                        },
                        "required": [
                          "query"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "google_serp",
                    "description": "Google search results as JSON: ranked organic results, shopping ads and product cards with prices. Cost: 10 credits per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "google_serp"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "query": {
                            "type": "string",
                            "description": "Search query (e.g. \"sofa set kuwait price\")."
                          },
                          "country": {
                            "type": "string",
                            "description": "ISO-2 country code (gl). Defaults to \"kw\".",
                            "default": "kw"
                          },
                          "language": {
                            "type": "string",
                            "description": "ISO-2 language code (hl). Defaults to \"en\".",
                            "default": "en"
                          },
                          "pages": {
                            "type": "integer",
                            "description": "Number of SERP pages to fetch (1–3). Each page is ~10 organic results.",
                            "default": 1,
                            "minimum": 1,
                            "maximum": 3
                          }
                        },
                        "required": [
                          "query"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "google_ads_keyword_planner",
                    "description": "Google Ads Keyword Planner — search volume, competition, CPC bids, and 12-month trend data. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "google_ads_keyword_planner"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "keyword": {
                            "type": "string",
                            "description": "Seed keyword(s), comma or newline separated (e.g. \"Sofa Set\")."
                          },
                          "geo_target_constant": {
                            "type": "string",
                            "description": "Geo target constant ID (e.g. 2792 for Turkey, 2840 for US).",
                            "default": "2792"
                          },
                          "language_constant": {
                            "type": "string",
                            "description": "Language constant ID (e.g. 1037 for Turkish, 1000 for English).",
                            "default": "1037"
                          },
                          "include_adult_keywords": {
                            "type": "boolean",
                            "description": "Include adult keywords in results. Defaults to false.",
                            "default": false
                          },
                          "keyword_plan_network": {
                            "type": "string",
                            "description": "GOOGLE_SEARCH (default) or GOOGLE_SEARCH_AND_PARTNERS.",
                            "enum": [
                              "GOOGLE_SEARCH",
                              "GOOGLE_SEARCH_AND_PARTNERS"
                            ],
                            "default": "GOOGLE_SEARCH"
                          }
                        },
                        "required": [
                          "keyword"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "amazon_search",
                    "description": "Amazon.com.tr search by any term — product cards plus optional Amazon vs third-party seller offers (AOD). Billed per request: 1 credit per results page plus 1 per product whose seller offers are read. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "amazon_search"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "query": {
                            "type": "string",
                            "description": "Search term (e.g. \"royal canin\" or a barcode)."
                          },
                          "pages": {
                            "type": "integer",
                            "description": "Number of search result pages to fetch (1–3), 1 credit each. Defaults to 1.",
                            "default": 1,
                            "minimum": 1,
                            "maximum": 3
                          },
                          "include_offers": {
                            "type": "boolean",
                            "description": "Fetch All Offers (AOD) for the first 8 products and split Amazon vs Amazon_Others. Adds 1 credit per product read (up to 8). Defaults to true.",
                            "default": true
                          }
                        },
                        "required": [
                          "query"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "trendyol_search",
                    "description": "Trendyol.com product search — title, brand, price, rating, and merchant info per listing. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "trendyol_search"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "query": {
                            "type": "string",
                            "description": "Search term (e.g. \"iphone 15\" or a barcode)."
                          },
                          "pages": {
                            "type": "integer",
                            "description": "Number of search result pages to fetch (1–3). Defaults to 1.",
                            "default": 1,
                            "minimum": 1,
                            "maximum": 3
                          },
                          "sort": {
                            "type": "string",
                            "description": "Sort order. Defaults to SCORE.",
                            "enum": [
                              "SCORE",
                              "PRICE_BY_ASC",
                              "PRICE_BY_DESC",
                              "MOST_RECENT",
                              "BEST_SELLER",
                              "MOST_FAVOURITE",
                              "MOST_RATED"
                            ],
                            "default": "SCORE"
                          }
                        },
                        "required": [
                          "query"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "trendyol_store_products",
                    "description": "Trendyol.com store/seller product listing by merchant id or store URL — listing only, no per-product detail requests. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "trendyol_store_products"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "store": {
                            "type": "string",
                            "description": "Merchant id (e.g. \"968\") or a Trendyol store URL containing \"mid=\"."
                          },
                          "pages": {
                            "type": "integer",
                            "description": "Number of store listing pages to fetch (1–3). Defaults to 1.",
                            "default": 1,
                            "minimum": 1,
                            "maximum": 3
                          },
                          "sort": {
                            "type": "string",
                            "description": "Sort order. Defaults to BEST_SELLER.",
                            "enum": [
                              "SCORE",
                              "PRICE_BY_ASC",
                              "PRICE_BY_DESC",
                              "MOST_RECENT",
                              "BEST_SELLER",
                              "MOST_FAVOURITE",
                              "MOST_RATED"
                            ],
                            "default": "BEST_SELLER"
                          }
                        },
                        "required": [
                          "store"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "trendyol_product_detail",
                    "description": "Trendyol.com single product detail with seller (buybox) price comparison across all merchants. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "trendyol_product_detail"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "product": {
                            "type": "string",
                            "description": "Product id (e.g. \"990828141\") or a full Trendyol product URL."
                          }
                        },
                        "required": [
                          "product"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  },
                  {
                    "title": "url_scraper",
                    "description": "Scrape any URL — Markdown, raw or cleaned HTML, links, every image the page references (grouped by where it was found), and screenshots. Blocked requests are retried automatically. Cost: 1 credit per run.",
                    "type": "object",
                    "required": [
                      "script_name",
                      "params"
                    ],
                    "additionalProperties": false,
                    "properties": {
                      "script_name": {
                        "const": "url_scraper"
                      },
                      "params": {
                        "type": "object",
                        "properties": {
                          "url": {
                            "type": "string",
                            "description": "Absolute URL to scrape (e.g. https://example.com/page).",
                            "format": "uri"
                          },
                          "formats": {
                            "type": "string",
                            "description": "Comma-separated output formats: markdown, raw_html, cleaned_html, links, images, screenshot, full_page_screenshot. Defaults to \"markdown\". images returns { src, alt, source, context } where source is img, meta, structured_data, css_background, data_attribute, link, icon, media or script. screenshot/full_page_screenshot require use_proxy=true.",
                            "default": "markdown"
                          },
                          "use_proxy": {
                            "type": "boolean",
                            "description": "Turn on network options: country/region/residential targeting and screenshots. Defaults to false; blocked requests are retried automatically either way.",
                            "default": false
                          },
                          "country": {
                            "type": "string",
                            "description": "ISO-2 proxy exit country (geoCode), e.g. \"us\", \"tr\". Defaults to any. Ignored unless use_proxy=true."
                          },
                          "region": {
                            "type": "string",
                            "description": "Proxy exit continent (regionalGeoCode), used when country is not set. Ignored unless use_proxy=true.",
                            "enum": [
                              "europe",
                              "asia",
                              "africa",
                              "oceania",
                              "northamerica",
                              "southamerica"
                            ]
                          },
                          "residential": {
                            "type": "boolean",
                            "description": "Use residential/mobile proxy network instead of datacenter. Defaults to false. Ignored unless use_proxy=true.",
                            "default": false
                          },
                          "device": {
                            "type": "string",
                            "description": "Viewport device profile: desktop, mobile, or tablet. Defaults to desktop.",
                            "enum": [
                              "desktop",
                              "mobile",
                              "tablet"
                            ],
                            "default": "desktop"
                          }
                        },
                        "required": [
                          "url"
                        ],
                        "additionalProperties": false
                      },
                      "async": {
                        "type": "boolean",
                        "description": "true → respond 202 with a correlation_id instead of waiting for the result."
                      }
                    }
                  }
                ],
                "discriminator": {
                  "propertyName": "script_name"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Completed run",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RunResult"
                }
              }
            }
          },
          "202": {
            "description": "Accepted (async: true). Poll GET /v1/runs/{correlationId}.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RunAccepted"
                }
              }
            }
          },
          "400": {
            "description": "Malformed body or unknown script_name",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "402": {
            "description": "Not enough credits",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Worker error (credit refunded)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Target site blocked the request (credit refunded)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "504": {
            "description": "Worker timed out (credit refunded)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/runs/{correlationId}": {
      "get": {
        "operationId": "getRun",
        "summary": "Status and result of a run",
        "parameters": [
          {
            "name": "correlationId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "since",
            "in": "query",
            "required": false,
            "description": "Only logs after this ISO 8601 time.",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "logs",
            "in": "query",
            "required": false,
            "description": "\"false\" to omit logs.",
            "schema": {
              "type": "string",
              "enum": [
                "true",
                "false"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Run status",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RunStatus"
                }
              }
            }
          },
          "404": {
            "description": "Run not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/scripts": {
      "get": {
        "operationId": "listScripts",
        "summary": "List every script with its parameters and credit cost",
        "responses": {
          "200": {
            "description": "Script registry"
          }
        }
      }
    },
    "/v1/scripts/{name}": {
      "get": {
        "operationId": "getScript",
        "summary": "One script with its parameters and credit cost",
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "google_keyword_suggestor",
                "google_serp",
                "google_ads_keyword_planner",
                "amazon_search",
                "trendyol_search",
                "trendyol_store_products",
                "trendyol_product_detail",
                "url_scraper"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Script definition"
          },
          "404": {
            "description": "Unknown script",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key"
      }
    },
    "schemas": {
      "RunResult": {
        "type": "object",
        "properties": {
          "correlation_id": {
            "type": "string",
            "format": "uuid",
            "description": "ID of this run."
          },
          "transaction_id": {
            "type": "string",
            "description": "ID of the credit charge for this run."
          },
          "script_name": {
            "type": "string"
          },
          "cost": {
            "type": "number",
            "description": "Credits charged."
          },
          "result": {
            "type": "object",
            "description": "The script’s output."
          },
          "balance_after": {
            "type": "number",
            "description": "Credit balance after the charge."
          }
        }
      },
      "RunAccepted": {
        "type": "object",
        "properties": {
          "correlation_id": {
            "type": "string",
            "format": "uuid"
          },
          "status": {
            "type": "string"
          }
        }
      },
      "RunStatus": {
        "type": "object",
        "properties": {
          "correlation_id": {
            "type": "string",
            "format": "uuid"
          },
          "status": {
            "type": "string"
          },
          "script_name": {
            "type": "string"
          },
          "result": {
            "type": "object"
          },
          "error_message": {
            "type": "string"
          },
          "logs": {
            "type": "array",
            "items": {
              "type": "object"
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "correlation_id": {
            "type": "string"
          },
          "refunded": {
            "type": "boolean"
          }
        }
      }
    }
  }
}
