Skip to main content

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.db and sends via AppleScript. Speaks JSON-RPC 2.0 over stdio.
  • pimsg — this package. Spawns imsg rpc as 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
  • imsg installed (brew install steipete/tap/imsg or 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pimsg-0.1.1.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pimsg-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pimsg-0.1.1.tar.gz.

File metadata

  • Download URL: pimsg-0.1.1.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pimsg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2fb23bf01cdf2f2575088c3fb794711345c229ef3b6d8f3d0911567353f12447
MD5 da57ce05ccadc33dc8438b2df22e883d
BLAKE2b-256 d3a59377fd2f1508d1d5c6f59b4d9fbe2f385c0bdbb25edd973275a9a0f46a2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pimsg-0.1.1.tar.gz:

Publisher: publish.yml on dynacylabs/pimsg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pimsg-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pimsg-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pimsg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2de1d678aecfb1118ba6815b557c225495a0d08a8cf074964f9c292cc5c398e
MD5 751b3d70fb602562dfec5dcd35f05583
BLAKE2b-256 914b15f139c80ea29399886b749ab085cf7bfc4dca82b9b1a94eeffc54c0ad4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pimsg-0.1.1-py3-none-any.whl:

Publisher: publish.yml on dynacylabs/pimsg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page