Call external APIs

HTTP Requests Overview

You can send HTTP requests to external services from your scripts using the requests package, which comes in two flavors:

Synchronous Requests:

Use synchronous requests for quick, lightweight API calls where you don’t expect noticeable delays. These are blocking calls—meaning the UI will pause until the request completes.

import requests

def fetch_data_sync():
    response = requests.request(url='https://api.example.com', method='GET')
    return response.json()

Note: Synchronous requests are best suited for fast operations. Long-running requests may freeze the editor temporarily.

Asynchronous Requests:

Use asynchronous requests for slower or more data-intensive operations. These requests prevent the UI from freezing but must be awaited using Python’s async/await syntax.

import requests.asyncs as requests

async def fetch_data():
    response = await requests.request(url='https://api.example.com', method='GET')
    return response.json()

Important: All methods in the requests.asyncs module are asynchronous. You must use await when calling them inside an async function.

Recommendation:

For operations that might:

  • Take more than a few seconds,
  • Involve heavy data processing, or
  • Need retries and orchestration logic,

Consider UsingOrchestrate instead. This offloads work from the browser session and improves user experience by handling long-running tasks on the backend without blocking the UI.