DEV Community

Alex Spinov
Alex Spinov

Posted on

Matrix Has a Free API — Heres How to Build Decentralized Chat Without Slack or Discord

Matrix is an open protocol for decentralized communication. Build chat apps, bots, and bridges to Slack/Discord/Telegram — all federated, all encrypted.

Why Matrix?

  • Decentralized: No single point of failure
  • Federated: Users on different servers can chat
  • E2E encrypted: Megolm protocol
  • Bridges: Connect to Slack, Discord, Telegram, IRC, Signal
  • Bots: Rich bot ecosystem
  • Self-hostable: Run your own homeserver
  • Standard protocol: Not controlled by any company

Self-Host with Synapse

docker run -d --name synapse \
  -v synapse-data:/data \
  -p 8008:8008 \
  -e SYNAPSE_SERVER_NAME=matrix.example.com \
  -e SYNAPSE_REPORT_STATS=no \
  matrixdotorg/synapse:latest generate

docker start synapse
Enter fullscreen mode Exit fullscreen mode

API: Register User

curl -X POST http://localhost:8008/_matrix/client/v3/register \
  -H 'Content-Type: application/json' \
  -d '{
    "username": "alice",
    "password": "secure-password",
    "auth": {"type": "m.login.dummy"}
  }'
Enter fullscreen mode Exit fullscreen mode

API: Login

curl -X POST http://localhost:8008/_matrix/client/v3/login \
  -H 'Content-Type: application/json' \
  -d '{
    "type": "m.login.password",
    "user": "alice",
    "password": "secure-password"
  }'
# Returns: { "access_token": "...", "user_id": "@alice:matrix.example.com" }
Enter fullscreen mode Exit fullscreen mode

API: Create Room

curl -X POST http://localhost:8008/_matrix/client/v3/createRoom \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "General",
    "topic": "General discussion",
    "preset": "private_chat"
  }'
Enter fullscreen mode Exit fullscreen mode

API: Send Message

curl -X PUT http://localhost:8008/_matrix/client/v3/rooms/ROOM_ID/send/m.room.message/txn1 \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "msgtype": "m.text",
    "body": "Hello, Matrix!"
  }'
Enter fullscreen mode Exit fullscreen mode

API: Read Messages

curl 'http://localhost:8008/_matrix/client/v3/rooms/ROOM_ID/messages?dir=b&limit=20' \
  -H 'Authorization: Bearer ACCESS_TOKEN'
Enter fullscreen mode Exit fullscreen mode

API: Invite User

curl -X POST http://localhost:8008/_matrix/client/v3/rooms/ROOM_ID/invite \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{"user_id": "@bob:matrix.example.com"}'
Enter fullscreen mode Exit fullscreen mode

Build a Bot (Node.js)

import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from 'matrix-bot-sdk';

const client = new MatrixClient(
  'http://localhost:8008',
  'BOT_ACCESS_TOKEN',
  new SimpleFsStorageProvider('bot-storage.json')
);

AutojoinRoomsMixin.setupOnClient(client);

client.on('room.message', (roomId, event) => {
  if (event.content.body?.startsWith('!ping')) {
    client.sendMessage(roomId, { msgtype: 'm.text', body: 'Pong!' });
  }
});

await client.start();
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case

A company replaced Slack ($12.50/user/mo × 50 users = $625/mo) with self-hosted Matrix + Element. They kept Slack bridge for external contacts, got E2E encryption for sensitive channels, and saved $7,500/year.


Need to automate data collection? Check out my Apify actors for ready-made scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)