> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AgentQL structured data extraction Tool

> Guide for using the AgentQL structured data extraction with PraisonAI agents.

AgentQL lets an agent extract structured data from any webpage using a natural-language prompt.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "AgentQL"
        User[User] --> Agent[Agent]
        Agent --> Tool[extract_web_data_tool]
        Tool --> API[AgentQL API]
        API --> Result[Structured Data]
    end

    classDef input fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class User,Agent input
    class Tool,API process
    class Result output
```

## Overview

AgentQL is a tool that allows you to extract structured data from webpages using AI Agents.

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
pip install langchain_agentql langchain-community
```

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
export AGENTQL_API_KEY="${AGENTQL_API_KEY:?Set AGENTQL_API_KEY in your shell}"
```

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent, AgentTeam
from langchain_agentql.tools import ExtractWebDataTool
from dotenv import load_dotenv

import os

os.environ["AGENTQL_API_KEY"] = os.getenv('AGENTQL_API_KEY')

def extract_web_data_tool(url, query):
    agentql_tool = ExtractWebDataTool().invoke(
        {
            "url": url,
            "prompt": query,
        },)
    return agentql_tool

# Create agent with web extraction instructions
orchestration_agent = Agent(
    instructions="""Extract All 37 products from the url https://www.colorbarcosmetics.com/bestsellers along with its name, overview, description, price and additional information by recursively clicking on each product""",
    tools=[extract_web_data_tool]
)

# Initialize and run agents
agents = AgentTeam(agents=[orchestration_agent])
agents.start()
```

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant AgentQL as extract_web_data_tool
    participant API as AgentQL API

    User->>Agent: "Extract products from URL"
    Agent->>AgentQL: extract_web_data_tool(url, prompt)
    AgentQL->>API: HTTPS request
    API-->>AgentQL: structured JSON
    AgentQL-->>Agent: parsed result
    Agent-->>User: final answer
```

## Getting Started

<Steps>
  <Step title="Simple Usage">
    1. Get your AgentQL API key from [AgentQL Dashboard](https://agentql.com)
    2. Set the API key in your environment variables
    3. Install the required dependencies
    4. Use the example code to start extracting structured data
  </Step>

  <Step title="With Configuration">
    Use the same tool with an agent — see **Usage with Agent** below, or pass env vars and options from the sections above.
  </Step>
</Steps>

## Best Practices

<AccordionGroup>
  <Accordion title="Keep AGENTQL_API_KEY in the environment">
    Set `AGENTQL_API_KEY` in your shell or `.env` and read it with `os.getenv`. Never commit the key in code.
  </Accordion>

  <Accordion title="Write precise prompts">
    AgentQL extracts exactly what the prompt asks for. Name every field you need (name, price, description) so the returned schema is predictable and token-efficient.
  </Accordion>

  <Accordion title="Handle extraction failures">
    Pages change and requests can time out. Wrap `ExtractWebDataTool().invoke(...)` in `try/except` so the agent can retry or report a clean message instead of crashing.
  </Accordion>
</AccordionGroup>

## Related Tools

<CardGroup cols={2}>
  <Card title="Firecrawl" icon="book" href="/docs/tools/external/firecrawl">
    Web scraping
  </Card>

  <Card title="Crawl4AI" icon="book" href="/docs/tools/external/crawl4ai">
    AI web crawler
  </Card>

  <Card title="GitHub" icon="book" href="/docs/tools/external/github">
    Repository automation
  </Card>
</CardGroup>
