XF 2.1 REST API

Welcome to another HYS for 2.1 and this one's a biggie. If you haven't seen the previous entries about what's coming in XF 2.1, check them out here.

Although Halloween may be over, why not trick the "Watch forum" link into giving you a treat, by getting it send you an email whenever we post about new things in the pipeline.

As I write this, our most popular suggestion was a REST API and with 2.1, it's here. While this is a fairly developer-focused feature on its own, it opens up many more integration options. This will make it easier to get data into or out of XenForo, without having to understand the underlying PHP framework that XF is built on.

The API breaks down into a few distinct components, so let's look at those in turn.
 
@Mike I realize the API is still in a beta and probably not done, and there's not yet any documentation. However, digging through the beta 5 Api controllers it doesn't seem like there'll be an endpoint to retrieve alerts for a user. Is this something that will be left out, or is expected to be in the final Api build? Also possible I'm just not finding it.
 
There isn't an endpoint for that currently, so it'd be worth making a specific suggestion about it. It might appear in a future build (though the API is also extendable if you have need for a particular endpoint).
 
How can i use api for xenforo 2.1 ? . i've test it and have error "No API key was included in the request".
Can we have a document for API xenforo 2 or late ?
 
. i've test it and have error "No API key was included in the request".

I'll take the educated guess that you don't have an API key in your request.

 
I'll take the educated guess that you don't have an API key in your request.

Thanks . I found it
 
Last edited:
API routes

As this is a REST-style API, the content you access and the action taken are based on the URL and the HTTP method used. We primarily use GET, POST and DELETE.

There are numerous routes in the API so we won't go into most of them in detail here. Full details of the available routes and their inputs and outputs will be found in the API documentation. (There are tools built-in to help with automatic documentation generation that add-on developers can also use for their own API routes.)

Let's look at /api/threads/123/ as an example. When you request this URL, what happens depends on the method used:
  • GET - this will get information about the thread with ID 123. If you pass the with_posts parameter, we'll also include a page of posts.
  • POST - this will update the thread's information, such as the title or whether it's sticky. Any elements you don't want to update can be omitted as parameters. Note that this is not for replying to this thread.
  • DELETE - as you might expect, this allows you to delete the thread. Parameters can be passed in to control the type of deletion (hard/soft), the reason provided, etc.
Each route may also have more specific actions. For example, GET /api/threads/123/posts?page=2 will get the second page of posts for this thread.

Here is some example output from GET /api/threads/2/:

JSON:
{
    "thread": {
        "thread_id": 2,
        "node_id": 2,
        "title": "Test API thread",
        "reply_count": 2,
        "view_count": 19,
        "user_id": 1,
        "username": "Example",
        "post_date": 1538157093,
        "sticky": false,
        "discussion_state": "visible",
        "discussion_open": true,
        "discussion_type": "",
        "first_post_id": 3,
        "last_post_date": 1540908968,
        "last_post_id": 21,
        "last_post_user_id": 1,
        "first_post_reaction_score": 0,
        "prefix_id": 0,
        "Forum": {
            "node_id": 2,
            "title": "Main forum",
            "node_name": null,
            "description": "",
            "node_type_id": "Forum",
            "parent_node_id": 1,
            "display_order": 100,
            "display_in_list": true,
            "breadcrumbs": [
                {
                    "node_id": 1,
                    "title": "Main category",
                    "node_type_id": "Category"
                }
            ],
            "type_data": {
                "allow_posting": true,
                "allow_poll": true,
                "require_prefix": false,
                "min_tags": 0,
                "discussion_count": 6,
                "message_count": 20,
                "last_post_id": 22,
                "last_post_date": 1540982158,
                "last_post_username": "test",
                "last_thread_id": 3,
                "last_thread_title": "Test API thread",
                "last_thread_prefix_id": 0,
                "can_create_thread": true,
                "can_upload_attachment": true
            }
        },
        "User": {
            "user_id": 1,
            "username": "Example",
            "email": "example@example.com",
            "visible": true,
            "activity_visible": true,
            "user_group_id": 2,
            "secondary_group_ids": [
                3,
                4
            ],
            "message_count": 16,
            "register_date": 1536666416,
            "trophy_points": 0,
            "user_state": "valid",
            "is_moderator": true,
            "is_admin": true,
            "is_staff": true,
            "is_banned": false,
            "reaction_score": 0,
            "custom_title": "",
            "warning_points": 0,
            "is_super_admin": true,
            "user_title": "Administrator",
            "age": 43,
            "dob": {
                "year": 1975,
                "month": 1,
                "day": 1
            },
            "signature": "",
            "location": "Somewhere",
            "website": "http://xenforo.com",
            "last_activity": 1540988599,
            "avatar_urls": {
                "o": null,
                "h": null,
                "l": null,
                "m": null,
                "s": null
            },
            "custom_fields": {
                "skype": "",
                "facebook": "",
                "twitter": ""
            },
            "is_ignored": false,
            "is_following": false,
            "can_edit": true,
            "can_ban": false,
            "can_warn": false,
            "can_view_profile": true,
            "can_view_profile_posts": true,
            "can_post_profile": true,
            "can_follow": false,
            "can_ignore": false,
            "can_converse": false
        },
        "is_watching": true,
        "visitor_post_count": 3,
        "can_edit": true,
        "can_edit_tags": false,
        "can_reply": true,
        "can_soft_delete": true,
        "can_hard_delete": false,
        "can_view_attachments": true
    }
}

These are derived from the entity system. For the most part, the top-level entry is a Thread entity, but you can also see a node (Forum) and a User. The API structure of a particular entity is defined by opting-in to specific fields and by a new method that allows you to define other custom elements to include (such as the results of the various can_xyz outputs).
Hi Chris ,

How can i creat a new thread ? .Can U show me document gui for creat thread ? .
Thanks U verymuch.
 
Support can only be provided to licensed customers so please create a new thread in the correct forum for assistance.
 
Interesting feature.
But what about embed media and attachment on post? Does the engine auto convert it to HTML or client app need to parse the message content to display it correctly?
If client need to parse the message body to display, it would be a pain.
 
This is the work-in-progress version of the documentation, which is generated via documentation in the code (and thus anyone can generate it via a CLI command, including for add-ons that add their own routes):


Note that this is more of a temporarily location for this page.
 
Is there any info out where which functions the api will support? Currently integrating Xenforo with our app backend. To login with the Xenforo credentials and also execute some extra features depending on our active UserUpgrades. Would be nice if we would fetch this information from an api instead of cross-database joining and then applying some regex to get the password hash etc.
 
Top Bottom