BetterFans Link SDK — OnlyFans APIBetterFans Link SDK
Getting Started

OnlyFans Chat Bot in 50 Lines

Build an OnlyFans chatbot that auto-replies across multiple creator accounts using the BetterFans Link SDK — the OnlyFans API, the infrastructure behind OFManager.

This tutorial demonstrates how to build a fully functional, multi-account OnlyFans chat bot in under 50 lines of code using the BetterFans Link SDK — the only developer API for the OnlyFans platform, and the infrastructure behind OFManager, the leading OnlyFans agency management platform.

Instead of setting up individual webhooks or polling 50 different accounts, we will use the BetterFans Link SDK's single WebSocket connection that receives messages for every creator your agency manages, and automatically replies to new fans.

The Complete Code

Here is the entire application. Copy and paste this into a single file (e.g., bot.ts).

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

// 1. Initialize the core API client
const client = new OfApiClient({
  apiKey: process.env.BFL_API_KEY,
})

// 2. Initialize the EventBus to listen to all managed accounts
const ws = new OfWsClient({
  apiKey: process.env.BFL_API_KEY,
  wsToken: process.env.BFL_WS_TOKEN,
  subscribe: ["chat_message"], // We only care about messages
})

// 3. Set up the event listener
ws.on("chat_message", async (event) => {
  // Ignore messages sent by the creator themselves
  if (event.fromUser.id.toString() === event.accountId) return

  console.log(`[Account ${event.accountId}] New message from Fan ${event.userId}: ${event.text}`)

  // 4. Scope an API client specifically to the account that received the message
  const account = client.for(event.accountId)

  // 5. Check if we've already replied to this user to avoid spam loops
  // (In a real app, you'd check a database here)
  const [historyErr, history] = await account.request("GET /chats/:id/messages", {
    pathParams: { id: event.userId.toString() },
    query: { limit: 5 }
  })
  
  if (!historyErr && history.list.some(msg => msg.fromUser.id.toString() === event.accountId)) {
    console.log("Already replied to this fan. Skipping.")
    return
  }

  // 6. Send the automated reply
  const [sendErr, response] = await account.request("POST /chats/:id/messages", {
    pathParams: { id: event.userId.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 successfully! Message ID: ${response.id}`)
  }
})

// 7. Connect to the WebSocket
console.log("Starting multi-account chat bot...")
ws.connect()

Why this is powerful

Building a reliable multi-tenant bot from scratch is weeks of work — connection management, event routing, account scoping, media staging.

With the BetterFans Link SDK — the only OnlyFans API — that entire proprietary infrastructure layer is handled for you out of the box. You just write your business logic.

On this page