Agentic Browser

AI agent for web automation and scraping, designed to plan, execute, and verify actions across any website

API server

Introduction#

REST surface for GitHub, Gmail, Calendar, YouTube, Website, and Health. Routers group by service; Pydantic models live under a shared models package.

Project structure#

The API server is initialized in a central module and mounts routers under prefixed paths. Routers define endpoints and request/response models, while services encapsulate business logic.

Core components#

  • FastAPI Application: Initializes the server with metadata and mounts routers under service-specific prefixes.
  • Routers: Define endpoints per service, handle request validation, and delegate to services.
  • Services: Encapsulate external integrations and business logic.
  • Models: Pydantic models define request/response schemas and validation.

Key initialization and mounting points:

  • Application creation and router inclusion are defined in the main API module.
  • Routers are exported via the routers package for centralized imports.

Architecture overview#

Split across these pieces:

  • Presentation Layer: FastAPI routes and request validation.
  • Domain Layer: Services orchestrate tool integrations.
  • Data Contracts: Pydantic models enforce schema and validation.

Detailed component analysis#

Health endpoint#

  • Method: GET
  • Path: /api/genai/health
  • Authentication: Not required
  • Request: No body
  • Response: HealthResponse
  • Fields: status (string), message (string)
  • Error Handling: None defined; returns success payload

GitHub endpoint#

  • Method: POST
  • Path: /api/genai/github
  • Authentication: Not required
  • Request Model: GitHubRequest
  • Fields:
  • url: HttpUrl (required)
  • question: string (required)
  • chat_history: array of dicts (optional, default [])
  • attached_file_path: string or null (optional)
  • Response Model: GitHubResponse
  • Fields:
  • content: string
  • Validation Rules:
  • url must be a valid HTTP(S) URL
  • question must be present
  • Error Handling:
  • Returns HTTP 400 if required fields are missing
  • Returns HTTP 500 for internal errors; logs error details

Gmail endpoints#

  • Method: POST
  • Paths:
  • /api/gmail/unread
  • /api/gmail/latest
  • /api/gmail/mark_read
  • /api/gmail/send
  • Authentication: Requires access_token in request body for all endpoints
  • Request Models:
  • UnreadRequest: access_token (required), max_results (optional, default 10)
  • LatestRequest: access_token (required), max_results (optional, default 5)
  • MarkReadRequest: access_token (required), message_id (required)
  • SendEmailRequest: access_token (required), to (required), subject (required), body (optional)
  • Response Models:
  • All endpoints return JSON objects with service-specific keys
  • Validation Rules:
  • access_token is required for all endpoints
  • max_results must be positive; defaults applied if omitted or invalid
  • mark_read requires message_id
  • send requires to and subject
  • Error Handling:
  • Returns HTTP 400 for missing required fields
  • Returns HTTP 500 for unexpected errors; logs exception details

Calendar endpoints#

  • Method: POST
  • Paths:
  • /api/calendar/events
  • /api/calendar/create
  • Authentication: Requires access_token in request body for both endpoints
  • Request Models:
  • EventsRequest: access_token (required), max_results (optional, default 10)
  • CreateEventRequest: access_token (required), summary (required), start_time (required, ISO 8601 string), end_time (required, ISO 8601 string), description (optional, default)
  • Validation Rules:
  • access_token is required
  • max_results must be positive; defaults applied if omitted or invalid
  • start_time and end_time must be valid ISO 8601 strings
  • Error Handling:
  • Returns HTTP 400 for missing or invalid fields
  • Returns HTTP 500 for unexpected errors; logs exception details

YouTube endpoint#

  • Method: POST
  • Path: /api/genai/youtube
  • Authentication: Not required
  • Request Model: AskRequest
  • Fields:
  • url: string (required)
  • question: string (required)
  • chat_history: array of dicts (optional, default [])
  • attached_file_path: string or null (optional)
  • Response: JSON object containing an answer field
  • Validation Rules:
  • url and question are required
  • Error Handling:
  • Returns HTTP 400 for missing required fields
  • Returns HTTP 500 for internal errors; logs error details

Website endpoint#

  • Method: POST
  • Path: /api/genai/website
  • Authentication: Not required
  • Request Model: WebsiteRequest
  • Fields:
  • url: string (required)
  • question: string (required)
  • chat_history: array of dicts (optional, default [])
  • client_html: string or null (optional)
  • attached_file_path: string or null (optional)
  • Response: JSON object containing an answer field
  • Validation Rules:
  • url and question are required
  • Error Handling:
  • Returns HTTP 400 for missing required fields
  • Returns HTTP 500 for internal errors; logs error details

Dependency analysis#

  • Router-to-Service Coupling:
  • Each router depends on a dedicated service class injected via FastAPI Depends.
  • Services depend on tool modules for external integrations.
  • Cross-Router Cohesion:
  • Routers group by domain and share few cross-dependencies.
  • External Dependencies:
  • Services rely on external APIs/tools; errors propagate as HTTP 500 with logged details.

Performance considerations#

  • Validation Early Exit: Routers validate required fields and return HTTP 400 promptly to avoid unnecessary work.
  • Defaults for Pagination: Endpoints default max_results to safe values when omitted or invalid.
  • Logging: Routers and services log errors; ensure structured logging is configured for production observability.
  • Asynchronous Workflows: GitHub endpoint supports async processing; ensure the underlying tooling is efficient and consider timeouts.

Troubleshooting guide#

  • Common HTTP 400 Errors:
  • Missing access_token or message_id in Gmail/Calendar endpoints.
  • Missing url or question in YouTube/Website/GitHub endpoints.
  • Invalid ISO 8601 timestamps in Calendar create endpoint.
  • Internal HTTP 500 Errors:
  • Unexpected exceptions are caught and returned as HTTP 500 with a generic message; check server logs for stack traces.
  • Authentication Notes:
  • Access tokens are passed in request bodies for Gmail and Calendar endpoints; ensure clients supply valid tokens.
  • Debugging Tips:
  • Enable server-side logging to capture request validation failures and service exceptions.
  • Use curl or Postman to test endpoints with minimal payloads to isolate issues.

Conclusion#

Call the mounted prefixes, send the fields each request model expects, and treat 4xx bodies as validation truth. Services wrap the external SDKs so routers stay thin.

Appendices#

API versioning and compatibility#

  • Current Version: The application declares a version in the FastAPI metadata.
  • Recommendations:
  • Add a version prefix to router paths (e.g., /api/v1/.) to support multiple versions concurrently.
  • Introduce deprecation headers and a changelog for breaking changes.
  • Maintain backward compatibility windows with clear deprecation timelines.

Security considerations#

  • Token Handling:
  • Gmail and Calendar endpoints require access_token in request bodies; treat them as sensitive credentials.
  • Avoid logging raw tokens; sanitize logs and consider token masking.
  • Transport Security:
  • Deploy behind HTTPS termination; enforce TLS in production.
  • Input Sanitization:
  • Validate and sanitize inputs; consider rate limiting and request size caps.
  • Authorization:
  • For endpoints requiring broader authorization, integrate middleware or API keys at the gateway level.

Rate limiting#

  • Recommendation:
  • Implement rate limiting at the gateway or via middleware to protect downstream tools.
  • Use sliding window or token bucket algorithms; expose quota headers when possible.

Monitoring endpoints#

  • Health Endpoint:
  • Use the existing health endpoint for liveness/readiness probes.
  • Metrics:
  • Expose metrics via a separate endpoint or middleware for latency, error rates, and throughput.

Administrative interfaces#

  • Recommendations:
  • Provide admin endpoints for diagnostics, queue inspection, and configuration updates.
  • Secure admin endpoints with authentication and authorization controls.

Client implementation guidelines#

  • Base URL:
  • Use the mounted router prefixes as base paths for each service.
  • Request Bodies:
  • Supply required fields as defined by each endpoint’s request model.
  • Error Handling:
  • Clients should parse HTTP 400 responses for validation errors and HTTP 500 for server errors.
  • Example Patterns:
  • For YouTube/Website/GitHub: send url and question; optionally include chat_history and attached_file_path.
  • For Gmail/Calendar: include access_token and any additional required fields.