← Code Exchange

Temporal + Browserbase Stagehand Integration

A best practices example showing how Temporal handles browser automation failures with automatic retries using atomic, idempotent activities.

What it does#

  • Uses Stagehand to perform Google searches in a real browser
  • Each individual task is encapsulated within a Temporal Activity following best practices for atomicity and idempotency
  • If any individual task fails, Temporal will automatically retry it, resulting in reliable browser automation
  • Clean, maintainable code following Temporal patterns

Temporal Best Practices Demonstrated#

Atomic Activities#

Each Temporal activity performs a single, well-defined task:

  1. initializeBrowser - Creates and initializes browser session
  2. navigateToSearchPage - Navigates to Google
  3. executeSearch - Types query and submits search
  4. extractSearchResults - Extracts and validates results
  5. cleanupBrowser - Closes browser session
  6. formatResults - Formats results for display

Why Atomic Activities?#

  • Efficient retries: If extraction fails after search succeeds, only extraction is retried
  • Better performance: No need to repeat successful steps
  • Clearer debugging: Each activity's purpose is obvious
  • Flexible retry policies: Different activities can have different retry strategies

Idempotent Design#

  • Browser sessions are reused if already initialized
  • Cleanup handles already-closed sessions gracefully
  • Navigation always results in the same state
  • Formatting produces consistent output

Language:

TypeScript

TypeScript

🤝 Partner
browserautomation

About the Author

Browserbase logo

Browserbase