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

# Workspace: Grep

> Search for a regex pattern across files in the agent's workspace

Search for a regex pattern across files in the per-agent workspace, with optional file-type filtering and surrounding context lines.

## Path Parameters

<ParamField path="agent_id" type="string" required>
  Agent ID (UUID)
</ParamField>

## Request Body

<ParamField body="pattern" type="string" required>
  Regular expression to search for.
</ParamField>

<ParamField body="path" type="string">
  Directory or file path to search, relative to the workspace root.
</ParamField>

<ParamField body="include" type="string">
  Optional filename glob to include (e.g. `*.py`).
</ParamField>

<ParamField body="context_lines" type="integer" default={0}>
  Number of context lines to include before and after each match.
</ParamField>

## Response

<ResponseField name="status" type="string">
  Result status (e.g., `ok`).
</ResponseField>

<ResponseField name="matches" type="array">
  Array of match objects, each including the file path, matching line, and surrounding context.
</ResponseField>

## Example Request

```bash theme={"dark"}
curl -X POST "https://api.xpander.ai/v1/agents/<agent-id>/workspace/grep" \
  -H "Content-Type: application/json" \
  -H "x-api-key: <your-api-key>" \
  -d '{
    "pattern": "def main",
    "path": "src",
    "include": "*.py",
    "context_lines": 2
  }'
```

## Notes

* `pattern` is a regex. Escape regex metacharacters if you want to match them literally.
* Narrow the search with `path` and `include` for faster results on large workspaces.

## See Also

* [Workspace: Glob](/api-reference/v1/agents/workspace/glob)
* [Workspace: File Read](/api-reference/v1/agents/workspace/file-read)


## OpenAPI

````yaml POST /v1/agents/{agent_id}/workspace/grep
openapi: 3.1.0
info:
  title: xpander.ai API Service
  description: |2-

        The xpander.ai API Service provides a unified REST API for managing AI agents,
        executing tasks, managing knowledge bases, and integrating with external systems.
        
        Features:
        - Agent Management: Create, update, deploy, and delete AI agents
        - Task Execution: Invoke agents with support for sync, async, and streaming modes
        - Knowledge Bases: Manage knowledge bases and documents for RAG workflows
        - Tools: Discover, connect, and attach tools (connectors, custom functions, MCP servers, sub-agents, workflows) to agents and workflows
        - MCP Integration: Model Context Protocol support for standardized AI interactions
        
        Authentication: All endpoints require authentication via either an API key (`x-api-key`) or an OAuth2 JWT (`Authorization: Bearer <jwt>`).
        
  version: '0.001'
servers:
  - url: https://api.xpander.ai
security: []
paths:
  /v1/agents/{agent_id}/workspace/grep:
    post:
      tags:
        - API v1
        - Agents
        - Agents Workspace
      summary: 'Workspace: Grep'
      description: Search for a regex pattern across files in the agent's workspace.
      operationId: Workspace__Grep_v1_agents__agent_id__workspace_grep_post
      parameters:
        - name: agent_id
          in: path
          required: true
          schema:
            type: string
            title: Agent Id
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GrepRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GrepResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - APIKeyHeader: []
components:
  schemas:
    GrepRequest:
      properties:
        pattern:
          type: string
          title: Pattern
          description: Regex pattern to search for (Extended Regular Expressions).
          examples:
            - def main
        path:
          type: string
          title: Path
          description: >-
            Directory or file path to search in, relative to workspace root.
            Directories are searched recursively.
          default: ''
          examples:
            - src
        include:
          anyOf:
            - type: string
            - type: 'null'
          title: Include
          description: >-
            Glob pattern to filter which files to search (e.g. '*.py' to only
            search Python files).
          examples:
            - '*.py'
        context_lines:
          type: integer
          maximum: 20
          minimum: 0
          title: Context Lines
          description: Number of lines of context to show before and after each match.
          default: 2
      type: object
      required:
        - pattern
      title: GrepRequest
    GrepResponse:
      properties:
        matches:
          items:
            $ref: '#/components/schemas/GrepMatch'
          type: array
          title: Matches
          description: List of matched results with context
        total_matches:
          type: integer
          title: Total Matches
          description: Total number of matching lines found
          default: 0
      type: object
      title: GrepResponse
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    GrepMatch:
      properties:
        file:
          type: string
          title: File
          description: Path of the file containing the match
        line:
          type: integer
          title: Line
          description: 1-indexed line number of the match
        content:
          type: string
          title: Content
          description: The matched line content
        context:
          type: string
          title: Context
          description: Surrounding lines of context
          default: ''
      type: object
      required:
        - file
        - line
        - content
      title: GrepMatch
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      description: API Key for authentication
      in: header
      name: x-api-key

````