Step 1: Get your API key

Before we get started, you’ll need to get an API key for your Whop. If you do not have a Whop already, create one here.

Then get the API key for your agent. Instructions available here.


Step 2: Read DMs on demand

Choose which DMs feed you want to read. We’ll need the id of the feed, which you can find in the URL of the feed. Feed IDs are formatted like this: feed_123.

Set the headers for the request to include your API key.

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {WHOP_APP_API_KEY}",
    "x-on-behalf-of": WHOP_AGENT_USER_ID
}

If you want to read your DMs instead of the agent’s, you can switch the header to your own user ID.

"x-on-behalf-of": WHOP_USER_USER_ID

Now, we can read fetch a DM.

full_query = """query DmsFeedData($feedId: ID!, $postsLimit: Int!) {
    dmsFeedData(feedId: $feedId, postsLimit: $postsLimit) {
          posts {
            content
            user {
                username
                id
            }
        }
    }
}"""

payload = {
    "query": full_query, 
    "variables": {
        "feedId": feed_id, 
        "postsLimit": 1
    }
}

response = requests.post(
    "https://api.whop.com/public-graphql", headers=headers, json=payload
)
response_json = response.json()
print(response_json)

if len(response_json["data"]["dmsFeedData"]["posts"]) > 0:
    latest_post = response_json["data"]["dmsFeedData"]["posts"][0]
    message_content = latest_post["content"]
    print(latest_post)
else:
    print("No DMs found")

This will fetch the most recent DM from the feed. You can change the postsLimit to fetch more or fewer DMs.

We are fetching the content of the most recent DM, as well as the user that sent it’s username and ID. There are many more fields available, you can see the full list here.

Let’s include some logic to make sure that the most recent DM is from the other user.

user_id = latest_post["user"]["id"]

if user_id != WHOP_AGENT_USER_ID:
    print("Last DM sent from the agent")
    return {"message": "Last DM sent from the agent"}
else:
    print("Last DM sent from the user, continuing...")

Step 3: Generate a response

Connect to an LLM to generate a response to the DM. In this example, we’ll use the openai library.

pip install openai
export OPENAI_API_KEY="sk-proj-..."
from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4.1",
    messages=[
        {
            "role": "user",
            "content": message_content
        }
    ]
)

response = completion.choices[0].message.content
print(response)

Great! Now we’ve fetched the latest DM, seen if it’s from the other user, and if so, generated a response.


Step 4: Sending the response back to Whop

Let’s send the response back to Whop.

full_query = """mutation sendMessage($input: SendMessageInput!) {
	sendMessage(input: $input)
}"""

payload = {
    "query": full_query,
    "variables": {
        "input": {
            "feedId": feed_id,
            "feedType": "dms_feed",
            "message": response
        }
    },
}

response = requests.post(
    "https://api.whop.com/public-graphql", 
    headers=headers,    # use the same headers as before
    json=payload
)

And we’re done! You should see your response in the DM feed.

Any time you want to send a response, you run this script.


Next Steps

What we’ve built is still pretty manual. We’re reading the DMs on demand, and sending a response each time.

We can plug into websockets to get real-time updates on DMs, and send responses as they come in, so no manual polling is needed.

More docs on how to do this coming soon!