What's new
DevAnswe.rs Forums

Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts.

XenForo and Python API, how to create a new thread?

codeMtim

New member
Firstly, you must have Python installed on your OS. Search online for how to install Python on Windows, Mac, Linux, etc. Once Python is installed:

Go to XenForo Admin > Setup > API Keys

Title:
Enter whatever here, e.g. Test API
Key type: User key (enter the forum username you want to post as)
Allowed scopes: Check thread:write (Covers creating, updating and soft-deleting threads and posts.)

Click Save. "The API key "Test API" has been created or updated. The key to use with the API is as follows:"

Copy the API Key.

Use this API key for your Python script.

Python:
import requests

api_key = 'your_api_key'
api_url = 'https://your_xenforo_site.com/api/threads'
headers = {'XF-Api-Key': api_key}
data = {
    'node_id': 1,  # The ID of the forum where you want to create the thread
    'title': 'Thread Title',
    'message': 'This is the content of the first post in the thread.'
}

response = requests.post(api_url, headers=headers, data=data)

if response.status_code == 200:
    print("Thread created successfully!")
else:
    print("Failed to create thread.")


Replace 'your_api_key' and 'https://your_xenforo_site.com' with the appropriate API key and your XenForo site URL. Adjust the node_id, title, and message parameters as needed.
 
Last edited:

codeMtim

New member
It seems like the requests library is not installed in your Python environment. The requests library is not part of the Python standard library, so you need to install it before using it.

To install the requests library, you can use the pip package installer. Open your terminal or command prompt and run the following command:

Code:
pip install requests
 
Top