FoxSecrets
Active member
How can I create custom API POST endpoint to my data?
I didn't find anything related to it in documentation.
I didn't find anything related to it in documentation.
src/XF/Api/Controller/*.php
). For your entities, you'll need to add setupApiResultData
methods and modify the structure to add 'api' => true
to the columns and relations you want to include in the results.Yes, I've seen that. But for example, how to setup a post request in case you have to send a body payload? How to receive it?Look under XF Admin -> Development -> Routes
There’s a tab for API (similar to Admin and Public routes).
Basically you define a route similar to other routes and then you have an API controller for that route.
Right. It seems a little complex to me. Any detailed documentation or even a real example of it?Yes, the API system is just another app (like the public or admin apps). Much of the same principles apply, only it renders API results instead of views and such. You can take a look at one of the core API controllers to get a feel for the differences (src/XF/Api/Controller/*.php
). For your entities, you'll need to addsetupApiResultData
methods and modify the structure to add'api' => true
to the columns and relations you want to include in the results.
Not really, other than the code itself. It's not too different from a regular controller, only the actions include the HTTP verbs:Right. It seems a little complex to me. Any detailed documentation or even a real example of it?
<?php
namespace Your\Api\Controller;
use XF\Api\Controller\AbstractController;
use XF\Api\Mvc\Reply\ApiResult;
use XF\Mvc\Entity\Entity;
use XF\Mvc\ParameterBag;
class Things extends AbstractController
{
protected function preDispatchController($action, ParameterBag $params): void
{
// assuming you've created the scope 'thing'
$this->assertApiScopeByRequestMethod('thing');
}
public function actionPost(): ApiResult
{
// your creation logic -- assuming $thing is the created entity
return $this->apiSuccess([
'thing' => $thing->toApiResult(Entity::VERBOSITY_VERBOSE)
]);
}
}
\XF\Api\Controller\Posts::actionPost
for a real-world example. It doesn't differ too much from \XF\Pub\Controller\Thread::actionAddReply
. If you have specific questions feel free to ask.I'm missing Dolores Park right about now...I’m walking around San Francisco like a tourist nerd right now, so I don’t have access to a computer to look up more detailed info, sorry.
Is that necessary?modify the structure to add'api' => true
to the columns and relations you want to include in the results
function setupApiResultData {}
$result->title= $this->title;
public function actionPost(ParameterBag $params): ApiResult
{
$this->assertRequiredApiInput(['sku', 'product', 'qty']);
$productSku = $this->filter('sku', 'uint');
if ($this->request->exists('sku') && $productSku)
{
$product = $this->assertProductExists($productSku);
}
else
{
$product = $this->em()->create('FOX\ProductManager:Product');
}
return $this->apiSuccess([
'product' => $product->toApiResult(Entity::VERBOSITY_VERBOSE)
]);
}
Not if you're setting it manually, but addingIs that necessary?
'api' => true
to the column or relation would accomplish the same thing. You could also do $result->includeColumn('title')
in the method.If the API key was passed incorrectly, you would get a different error (API key provided in request was not found). Does a basic request succeed (ie.So I've set scope and api url. So first question, where it goes the api key? I created a simple request and set XF-Api-Key on client side header but it returns "do_not_have_permission".
GET /api/index/
)?IsI also tried to get users as example with permission to user scope (http://localhost:8000/api/products/users/?page=1) and got "endpoint_not_found".
products/
in the route intentional? The regular end-point for users is /api/users/
. If you've created a custom products endpoint, that would map to the actionGetUsers
method of your corresponding controller.The same way as usual,How can I read the post params?
$this->filter('some_param', 'some_type')
.And how to respond with a json message?
return $this->apiSuccess([
'message' => \XF::phrase('some_message')
]);
Yes, it does (below), so the key is correct. Why can't I get users? /api/users/?page=1If the API key was passed incorrectly, you would get a different error (API key provided in request was not found). Does a basic request succeed (ie.GET /api/index/
)?
{
"version_id": 2020870,
"site_title": "XenForo",
"base_url": "http://localhost:8000",
"api_url": "http://localhost:8000/api/",
"key": {
"type": "guest",
"user_id": null,
"allow_all_scopes": true,
"scopes": []
}
}
As @Jeremy P said you have to enable the full member list in the options.Is the member list option enabled
Which option? Can you please tell me the path? I can't find itAs @Jeremy P said you have to enable the full member list in the options.
That one caught me out at first.
api_bypass_permissions
to false
in the request, and API requests tied to an account with administrative user permissions can access the users endpoint regardless of the option/permissions as well.public function actionGet(ParameterBag $params)
{
$this->assertApiScope('product:read');
$product = $this->assertViewableProduct($params->product_id);
$result = [
'product' => $product->toApiResult(Entity::VERBOSITY_VERBOSE)
];
return $this->apiResult($result);
}
We use essential cookies to make this site work, and optional cookies to enhance your experience.