How to find knowledge base answers via API

Hello everyone,

I’ve recently started working with the Zammad REST API and encountered some difficulties with the endpoints. I’ve been using developer tools and the network tab to identify the endpoint for a specific article. However, I still have some missing information that I need to make progress.

For instance, when I access the URL “https://imp.XXX.net/api/v1/knowledge_bases/1/categories/14,” it provides details about category 14, as shown below:

Now, my goal is to retrieve information about answer ID 6 from category ID 14 using the URL “https://imp.XXX.net/api/v1/knowledge_bases/1/categories/14/answers/6.” However, I’m encountering a 404 error with the message:

{
    "error": "No route matches [GET] /api/v1/knowledge_bases/1/categories/14/answers/6",
    "error_human": "No route matches [GET] /api/v1/knowledge_bases/1/categories/14/answers/6"
}

I would greatly appreciate any insights or guidance on how to resolve this issue and access the information I need. Thank you!

Researching the rails routes might help in some cases:

zammad run rails routes

ubuntu-rs@ubuntu-rs:/workspace/git_zammad/zammad$ rails routes | grep GET | grep knowledge_bases
                                    visible_ids_knowledge_bases GET                         /api/v1/knowledge_bases/visible_ids(.:format)                                                     knowledge_bases#visible_ids
                                 recent_answers_knowledge_bases GET                         /api/v1/knowledge_bases/recent_answers(.:format)                                                  knowledge_base/answers#recent_answers
                                              init_manage_index GET                         /api/v1/knowledge_bases/manage/init(.:format)                                                     knowledge_base/manage#init
                                         server_snippets_manage GET                         /api/v1/knowledge_bases/manage/:id/server_snippets(.:format)                                      knowledge_base/manage#server_snippets
                                                   manage_index GET                         /api/v1/knowledge_bases/manage(.:format)                                                          knowledge_base/manage#index
                                                     new_manage GET                         /api/v1/knowledge_bases/manage/new(.:format)                                                      knowledge_base/manage#new
                                                    edit_manage GET                         /api/v1/knowledge_bases/manage/:id/edit(.:format)                                                 knowledge_base/manage#edit
                                                         manage GET                         /api/v1/knowledge_bases/manage/:id(.:format)                                                      knowledge_base/manage#show
                                                    feed_tokens GET                         /api/v1/knowledge_bases/feed_tokens(.:format)                                                     knowledge_base/feed_tokens#show
                                                    permissions GET                         /api/v1/knowledge_bases/:id/permissions(.:format)                                                 knowledge_base/permissions#show
                                            feed_knowledge_base GET                         /api/v1/knowledge_bases/:id/:locale/feed(.:format)                                                knowledge_base/feeds#root
                                     knowledge_base_permissions GET                         /api/v1/knowledge_bases/:knowledge_base_id/categories/:id/permissions(.:format)                   knowledge_base/permissions#show
                                   feed_knowledge_base_category GET                         /api/v1/knowledge_bases/:knowledge_base_id/categories/:id/:locale/feed(.:format)                  knowledge_base/feeds#category
                                      knowledge_base_categories GET                         /api/v1/knowledge_bases/:knowledge_base_id/categories(.:format)                                   knowledge_base/categories#index
                                        knowledge_base_category GET                         /api/v1/knowledge_bases/:knowledge_base_id/categories/:id(.:format)                               knowledge_base/categories#show
                                          knowledge_base_answer GET                         /api/v1/knowledge_bases/:knowledge_base_id/answers/:id(.:format)                                  knowledge_base/answers#show
                                                 knowledge_base GET                         /api/v1/knowledge_bases/:id(.:format)                                                             knowledge_bases#show

This one looks promising:

curl -s -uadmin@example.com:test http://ubuntu-rs:3000/api/v1/knowledge_bases/1/answers/1 | jq .
{
  "id": 1,
  "assets": {
    "KnowledgeBaseAnswer": { ... },
    "KnowledgeBaseCategory": { ... },
    "KnowledgeBase": { ... },
    "KnowledgeBaseLocale": { ... },
    "KnowledgeBaseTranslation": { ... },
    "KnowledgeBaseCategoryTranslation": { ... },
    "KnowledgeBaseAnswerTranslation": { ... },
    "User": { ... },
    "Role": { ... },
    "Group": { ... }
  }
}

Thank you for your quick response. And I have already worked with the information that you have provided and it worked exactly as expected.

But I’d like to automate the process of retrieving answer IDs based on their corresponding category IDs. For example, if category ID 14 contains answer IDs 6, 7, 8, and 9, I expect that the URL “https://imp.XXX.net/api/v1/knowledge_bases/1/categories/14/answers/6” should work and return the content of answer ID 6.

Currently, I can manually retrieve the content of answer ID 6 using the URL “https://imp.XXX.net/api/v1/knowledge_bases/1/answers/6?include_contents=22,” but I want to automate this process by retrieving answer content directly from the category ID.

Any technical insights or guidance on how to achieve this automation would be greatly appreciated.

Hi @Banik ,
I’m not sure if this is currently possible. I was not involved in the knowledge base API, so I might just oversee things but in the controller it looks like that there is no “ALL” option for the include_contents.

If you are selfhosted, then you could try out my new private add-on:

It might be possible with that to get easier to your goal, here are some examples:

$ curl -s -u"admin@example.com:test" http://ubuntu-rs:3000/api/v1/tables/knowledge_base_answer_translations?answer_id=1 | jq .
[
  {
    "id": 1,
    "title": "test",
    "kb_locale_id": 1,
    "answer_id": 1,
    "content_id": 1,
    "created_by_id": 3,
    "updated_by_id": 3,
    "created_at": "2023-09-07T14:15:28.828Z",
    "updated_at": "2023-09-08T10:16:56.528Z"
  },
  {
    "id": 2,
    "title": "test DE",
    "kb_locale_id": 2,
    "answer_id": 1,
    "content_id": 2,
    "created_by_id": 3,
    "updated_by_id": 3,
    "created_at": "2023-09-08T10:16:56.501Z",
    "updated_at": "2023-09-08T10:16:56.529Z"
  }
]
$ curl -s -u"admin@example.com:test" http://ubuntu-rs:3000/api/v1/tables/knowledge_base_answer_translation_contents?id_in=1,2 | jq .
[
  {
    "id": 1,
    "body": "test"
  },
  {
    "id": 2,
    "body": "test DE"
  }
]