Intercom Python Library

repository·master·Indexed 42 days ago

https://github.com/intercom/python-intercom

A type-safe Python interface for Intercom APIs (version 5.0.1) supporting synchronous and asynchronous workflows via Intercom and AsyncIntercom clients. Features include management of AI content import sources, external pages, help center articles, admin activity, and data export jobs with built-in support for pagination, exponential backoff retries, and custom httpx clients.

Tokens
83K
Snippets
361
Records
401
Agent score
51%

What's inside Intercom

  1. Paginate through API results

    master

    Paginated requests return a SyncPager or AsyncPager. You can iterate through the items directly or iterate page-by-page using .iter_pages().

    When using .iter_pages(), you can access the typed response for each page via the .response attribute.

    from intercom import Intercom
    
    client = Intercom(
        token="YOUR_TOKEN",
    )
    response = client.articles.list()
    
    # Iterate through all items directly
    for item in response:
        yield item
    
    # Alternatively, paginate page-by-page
    for page in response.iter_pages():
        yield page
    
    # Access the typed response per page
    pager = client.articles.list(...)
    for page in pager.iter_pages():
        print(page.response)  # access the typed response for each page
        for item in page:
            print(item)
  2. Use the Intercom client

    master

    To use the synchronous client, instantiate Intercom with your API token. You can then call methods on the various API namespaces (e.g., ai_content).

    from intercom import Intercom
    
    client = Intercom(
        token="YOUR_TOKEN",
    )
    client.ai_content.create_content_import_source(
        url="https://www.example.com",
    )
  3. Manage and download data export jobs

    master

    Once a data export job is created, you can monitor its status, cancel it, or download the results.

    Check Job Status

    Use client.unstable.data_export.get_data_export(job_identifier=...) to view the status.

    • Expiry: Completed jobs are only available for download for two days after completion. After this, the data is no longer available.

    Cancel a Job

    Use client.unstable.data_export.cancel_data_export(job_identifier=...) to stop a pending job.

    Download Exported Data

    When the job status is complete, use client.unstable.data_export.download_data_export(job_identifier=...) to retrieve the data.

    • Format: Data is streamed as a gzipped CSV.
    • Requirement: The underlying endpoint requires the Accept: application/octet-stream header.
    from intercom import Intercom
    
    client = Intercom(token="YOUR_TOKEN")
    
    # Check status
    status = client.unstable.data_export.get_data_export(job_identifier="job_id")
    
    # Cancel job
    client.unstable.data_export.cancel_data_export(job_identifier="job_id")
    
    # Download data
    client.unstable.data_export.download_data_export(job_identifier="job_id")
  4. Paginate through article lists

    master

    When using list(), the returned SyncPager or AsyncPager allows you to iterate through items or access full pages.

    Sync Pagination:

    for item in response:  # Iterates through individual items
        yield item
    
    for page in response.iter_pages():  # Iterates through pages
        yield page

    Async Pagination:

    async for item in response:  # Iterates through individual items
        yield item
    
    async for page in response.iter_pages():  # Iterates through pages
        yield page
  5. Paginate through notes

    master

    When using client.notes.list(), the returned SyncPager or AsyncPager supports two ways of handling pagination:

    1. Item-based iteration: Iterate directly over the response object to get individual Note objects. This is the simplest way to process all notes.
    2. Page-based iteration: Use .iter_pages() to iterate through the response page-by-page. This is useful if you need to access metadata contained within the NoteList response for each page.

    Synchronous Example

    response = client.notes.list(contact_id="contact_id")
    
    # Iterate items
    for item in response:
        print(item)
    
    # Iterate pages
    for page in response.iter_pages():
        print(page)

    Asynchronous Example

    response = await client.notes.list(contact_id="contact_id")
    
    # Iterate items
    async for item in response:
        print(item)
    
    # Iterate pages
    async for page in response.iter_pages():
        print(page)
  6. Configure request retries

    master

    The SDK automatically retries requests with exponential backoff for retryable status codes: 408 (Timeout), 429 (Too Many Requests), and 5XX (Internal Server Errors).

    The default retry limit is 2. You can adjust this using the max_retries key in request_options.

    client.ai_content.create_content_import_source(..., request_options={
        "max_retries": 1
    })