API Reference
Complete API documentation for PyMessage functions.
Core Functions
Retrieve iMessage messages from iPhone backup database.
Retrieve iMessage messages from a chat database.
Query messages with optional filtering by phone numbers and date range. Returns a pandas DataFrame with message details, attachments, and reactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backup
|
A Backup object from find_backups(), or a direct path (str or Path) to a chat.db file (e.g. ~/Library/Messages/chat.db on macOS). Also accepts EXAMPLE_BACKUP for testing. |
required | |
phone_numbers
|
str | list[str] | None
|
Single phone number or list to filter conversations. Accepts various formats: "+1234567890", "(123) 456-7890", "email@example.com" |
None
|
date_range
|
tuple[str | datetime, str | datetime] | None
|
Tuple of (start, end) dates for filtering. Dates can be: - ISO format strings: "2024-01-01", "2024-12-31" - datetime objects If None, returns all messages. |
None
|
output_csv
|
str | Path | None
|
Optional path to export results as CSV. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If date_range has invalid format. |
FileNotFoundError
|
If specified path doesn't exist. |
Examples:
>>> from pymessage import find_backups, get_messages
>>> backups = find_backups()
>>> df = get_messages(backups[0])
List all conversations with summary statistics.
List all conversations with summary statistics.
Returns metadata about each conversation including participant count, message count, and date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backup
|
A Backup object specifying the data source. Use find_backups() to discover available sources, or EXAMPLE_BACKUP for testing. |
required | |
include_empty
|
bool
|
Include conversations with no messages (default False). |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If specified path doesn't exist. |
Examples:
Backup Management
Scan default macOS location for iPhone backups.
Scan for all available iMessage data sources.
Searches ~/Library/Application Support/MobileSync/Backup/ for iPhone backups and checks ~/Library/Messages/chat.db for the macOS Messages database.
Returns:
| Type | Description |
|---|---|
list[Backup]
|
List of Backup objects sorted by last backup date (most recent first), |
list[Backup]
|
with the macOS entry appended at the end if found. |
Examples:
Extract metadata from iPhone backup directory.
Extract metadata from iPhone backup directory.
Reads Info.plist and Manifest.plist to extract device information and backup details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backup_path
|
str | Path
|
Path to backup directory. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with backup metadata: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If backup_path doesn't exist. |
ValueError
|
If Info.plist is missing or malformed. |
Examples:
Attachments
Retrieve attachment metadata and file paths.
Retrieve attachment metadata and file paths.
Returns information about all attachments in conversations, optionally filtered by phone numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backup
|
A Backup object specifying the data source. Use find_backups() to discover available sources, or EXAMPLE_BACKUP for testing. |
required | |
phone_numbers
|
str | list[str] | None
|
Filter to attachments in these conversations. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If specified path doesn't exist. |
Examples:
Resolve attachment filename to actual path in backup.
Resolve attachment filename to actual path in backup.
iPhone backups store files using SHA1 hash of domain and relative path: path = SHA1("MediaDomain-" + relative_path) Structure: backup_root/[first_2_hex]/[full_hash]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Relative filename from attachment table. |
required |
backup_root
|
Path
|
Root directory of backup. |
required |
Returns:
| Type | Description |
|---|---|
Path | None
|
Absolute path to attachment file, or None if not found. |
Examples:
Utility Functions
Convert Apple timestamp to pandas Timestamp.
Convert Apple timestamp to pandas Timestamp.
Apple uses two timestamp formats: - Values >= 1 trillion: nanoseconds since 2001-01-01 - Values < 1 trillion: seconds since 2001-01-01
Zero values are treated as None (no timestamp).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
int | float | None
|
Apple timestamp value, or None. |
required |
Returns:
| Type | Description |
|---|---|
Timestamp | None
|
pandas Timestamp object in UTC, or None if input is None/zero. |
Examples:
Parse reaction/tapback type from associated_message_type.
Parse reaction/tapback type from associated_message_type.
Reactions are encoded as separate messages with specific type codes: - 2000-2007: Tapback added - 3000-3007: Tapback removed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
associated_message_type
|
int | None
|
Type code from message.associated_message_type. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Tuple of (reaction_type, action) where: |
str | None
|
|
tuple[str | None, str | None]
|
|
Examples:
Normalize phone number to digits-only format.
Normalize phone number to digits-only format.
Strips all non-digit characters except leading '+'. Email addresses (containing '@') are returned as-is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone
|
str
|
Phone number in any format, or email address. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Normalized phone number or email address. |
Examples:
Generate lookup variants for phone number matching.
Generate lookup variants for phone number matching.
Creates multiple representations to match against database, handling variations in how iMessage stores contact identifiers. Special handling for US +1 country code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone
|
str
|
Normalized phone number or email address. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of variants to try for lookup. Email addresses return |
list[str]
|
single-item list. |
Examples:
>>> variants = generate_phone_variants("+12345678900")
>>> set(variants) == {"+12345678900", "12345678900", "2345678900"}
True
>>> variants = generate_phone_variants("2345678900")
>>> "+12345678900" in variants
True
>>> "12345678900" in variants
True
>>> generate_phone_variants("user@example.com")
['user@example.com']
Analytics
Summary statistics across all messages.
Compute overall messaging activity statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame produced by get_messages(). |
required |
start
|
str | Timestamp | None
|
Optional start date for filtering (ISO string or Timestamp). |
None
|
end
|
str | Timestamp | None
|
Optional end date for filtering (ISO string or Timestamp). |
None
|
last_n_days
|
int | None
|
If provided, overrides start/end and filters to the last N days relative to reference_date. |
None
|
reference_date
|
Timestamp | None
|
Reference point for last_n_days. Defaults to now (UTC). |
None
|
top_n
|
int
|
Number of top contacts to include in top_contacts_df. |
10
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Tuple of (summary_df, top_contacts_df). |
DataFrame
|
summary_df is a single-row DataFrame with columns: |
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
|
tuple[DataFrame, DataFrame]
|
top_contacts_df has columns: contact, total, sent, received. |
tuple[DataFrame, DataFrame]
|
Sorted descending by total, limited to top_n rows. |
Examples:
Per-contact messaging statistics.
Compute per-contact messaging statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame produced by get_messages(). |
required |
contact
|
str
|
Phone number or email to summarize. All format variants are checked (e.g. "+12345678900", "2345678900"). |
required |
start
|
str | Timestamp | None
|
Optional start date for filtering. |
None
|
end
|
str | Timestamp | None
|
Optional end date for filtering. |
None
|
last_n_days
|
int | None
|
If provided, overrides start/end. |
None
|
reference_date
|
Timestamp | None
|
Reference point for last_n_days. Defaults to now (UTC). |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Single-row DataFrame with columns: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Examples:
Build a 7×24 message-count heatmap for a contact.
Build a 7×24 message-count heatmap for a contact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame produced by get_messages(). |
required |
contact
|
str
|
Phone number or email to filter on. |
required |
start
|
str | Timestamp | None
|
Optional start date for filtering. |
None
|
end
|
str | Timestamp | None
|
Optional end date for filtering. |
None
|
last_n_days
|
int | None
|
If provided, overrides start/end. |
None
|
reference_date
|
Timestamp | None
|
Reference point for last_n_days. Defaults to now (UTC). |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
7×24 DataFrame where: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Examples: