Python library and HTTP server for interacting with macOS Messages via imsg
Project description
pimsg
Python library and HTTP server for interacting with macOS iMessage and SMS via imsg.
Overview
pimsg wraps the imsg rpc JSON-RPC 2.0 subprocess interface to provide:
- Python library — typed, Pythonic access to chats, messages, sending, and live watching
- HTTP server — FastAPI-based REST + Server-Sent Events (SSE) API for use by web frontends or other services
imsg must be installed separately on the same macOS machine.
Architecture
pimsg is one layer in a three-tier stack:
[imsg] ──JSON-RPC 2.0──▶ [pimsg] ──HTTP/SSE──▶ [web interface]
Swift CLI this repo separate project
macOS only Python lib + Docker container
FastAPI server behind reverse proxy
- imsg — Swift binary, macOS only. Reads
~/Library/Messages/chat.dband sends via AppleScript. Speaks JSON-RPC 2.0 over stdio. - pimsg — this package. Spawns
imsg rpcas a subprocess, wraps it as a Python library and HTTP server. Also runs on the Mac. - web interface — a separate project (to be created) that consumes the pimsg HTTP API. Can run in Docker on the same Mac or elsewhere on the network.
Connecting Docker to pimsg
The web interface container talks to pimsg on the Mac via host.docker.internal:
Docker container
└── http://host.docker.internal:8765 → pimsg (Mac, bound to 0.0.0.0)
For a remote Docker host (different machine), use a Tailscale mesh or SSH tunnel — pimsg itself doesn't need to change. See INSTALL.md for details.
This is local-first software
pimsg only runs on macOS — it cannot be deployed to a cloud server or Linux machine. The Mac running pimsg must have imsg installed, Messages.app signed in, and Full Disk Access granted. The web interface is the only part that's network-portable.
Requirements
- macOS 14+ with Messages.app signed in
imsginstalled (brew install steipete/tap/imsgor built from source)- Full Disk Access granted to the terminal running pimsg
- Python 3.10+
Install
pip install pimsg
Or from source:
git clone https://github.com/dynacylabs/pimsg.git
cd pimsg
pip install -e ".[dev]"
Library Usage
import pimsg
# List chats
chats = pimsg.list_chats()
for chat in chats:
print(chat.name, chat.service)
# Get message history for a chat
messages = chat.messages(limit=50)
for message in messages:
print(message.sender, message.text)
# Send a message
pimsg.send(to="+15551234567", text="Hello from Python!")
# Watch for new messages (async generator)
async for message in pimsg.watch(chat_id=1):
print(message.sender, message.text)
HTTP Server Usage
# Start on default 127.0.0.1:8765
pimsg-server
# Bind to all interfaces (e.g. for Docker — ensure this connection is secured)
pimsg-server --host 0.0.0.0 --port 8765
Endpoints
| Method | Path | Description |
|---|---|---|
GET |
/chats |
List recent chats |
GET |
/chats/{id}/messages |
Message history for a chat |
POST |
/messages/send |
Send a message |
GET |
/chats/{id}/stream |
SSE stream of new messages |
GET |
/health |
Health check |
Security
The HTTP server is unauthenticated by default and binds to 127.0.0.1 only. This is intentional — auth is delegated to the user's infrastructure:
- Local Python use: no network involved, no risk.
- Same-Mac Docker: bind to
0.0.0.0, ensure the port is firewalled from external access. - Remote Docker / web interface: place pimsg behind a reverse proxy or VPN. The web interface project is responsible for end-user authentication.
See INSTALL.md for recommended network setups.
Development
pip install -e ".[dev]"
pytest
See DEVELOPMENT.md for the full development guide.
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pimsg-0.1.0.tar.gz.
File metadata
- Download URL: pimsg-0.1.0.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9136ccaa5c985fa8f31344d738b41adc5f3f4ba707242648e661d38ef535edc7
|
|
| MD5 |
0b69ef8f00a31edf887a20b037872d27
|
|
| BLAKE2b-256 |
31f22825afe6c034fccabf4e00b2c753a59a38bc4397a1190d62241dbc8c40ea
|
File details
Details for the file pimsg-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pimsg-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a22e072ac9b6bf47a37d4641eeb9823fa5b4d181c58ea9022b6fbd198bcf6db
|
|
| MD5 |
b69fb377710d339c4818c49b028139d1
|
|
| BLAKE2b-256 |
a7271304844e5bacb554370c68308c7dac213d5c622e6fb6301aa0ee296bf1ab
|