API docs / Chatbot tutorial

Getting Started

OnlyFans Chat Bot in 50 Lines

Build an OnlyFans chatbot that auto-replies to new fans across multiple creator accounts, using the OFManager API and live events.

This tutorial builds a working OnlyFans chat bot for several creators in about 50 lines. It listens for new messages on each creator's account and automatically replies to new fans.

Each creator gets its own live events connection, and replies go out through the same API key.

The complete code

Here is the entire application. Copy it into a single file (for example bot.ts) and put your creators' OnlyFans user IDs in the list.

bot.ts
import { OfApiClient, OfWsClient } from "@betterfans/link-sdk"

// 1. One API client for every request
const client = new OfApiClient({
  apiKey: process.env.OFM_API_KEY,
})

// 2. The creators the bot answers for (user IDs from the API Keys page)
const creators = ["111111111", "222222222"]

for (const creatorId of creators) {
  // 3. Scope API calls to this creator
  const account = client.for(creatorId)

  // 4. Open a live connection for this creator
  const ws = new OfWsClient({
    apiKey: process.env.OFM_API_KEY,
    auth: { onlyfansUserId: creatorId },
  })

  // 5. React to every new chat message
  ws.on("api2_chat_message", async (msg) => {
    const fanId = msg.fromUser?.id

    // Ignore messages the creator sent
    if (!fanId || fanId.toString() === creatorId) return

    console.log(`[${creatorId}] New message from fan ${fanId}: ${msg.text}`)

    // 6. Skip fans we've already replied to, to avoid loops
    // (In a real app, you'd check a database here)
    const [historyErr, history] = await account.request("GET /chats/:id/messages", {
      pathParams: { id: fanId.toString() },
      query: { limit: 5, order: "desc", skip_users: "all" },
    })

    if (!historyErr && history.list.some((m) => m.fromUser.id.toString() === creatorId)) {
      console.log("Already replied to this fan. Skipping.")
      return
    }

    // 7. Send the automated reply
    const [sendErr, response] = await account.request("POST /chats/:id/messages", {
      pathParams: { id: fanId.toString() },
      body: {
        text: "Hey! This is an automated welcome message. Thanks for reaching out!",
      },
    })

    if (sendErr) {
      console.error(`Failed to send message: ${sendErr.message}`)
    } else {
      console.log(`Reply sent! Message ID: ${response.id}`)
    }
  })

  ws.connect()
}

console.log(`Chat bot running for ${creators.length} creators`)

Why this is powerful

Building a reliable multi-account bot from scratch is weeks of work: connection management, sessions, proxies, reconnects and account scoping.

With the OFManager API, that infrastructure is handled for you. You just write your business logic.