Page MenuHomePhabricator

Create API call examples to create and edit Wikidata works and authors items.
Open, Needs TriagePublicFeature

Description

Feature summary: Create a new api-call-examples.md file with examples for creating and editing author work items in Wikidata, using the Wikibase REST API.

Specifically, create:

  • An example API call to create an author item, including a label, a description, and the statements "instance of (P31): human being (Q5)", "sex or gender (P21): female (Q6581072)", and "occupation (P106): writer (Q36180)".
  • An example API call to create a work item, including a label, a description, and the statements "instance of (P31): literary work (Q7725634)" and "form of creative work (P7937): novel (Q8261)".
  • An example API call to add a statement to the already created author item: "country of citizenship (P27): Uruguay (Q77)".
  • An example API call to modify the already created item's statement "country of citizenship (P27): Uruguay (Q77)" to "country of citizenship (P27): Bolivia (Q750)".

Documentation for using the Wikibase REST API is located here https://www.wikidata.org/wiki/Wikidata:REST_API and here https://doc.wikimedia.org/Wikibase/master/js/rest-api/

To test item creation and editing with the REST API, use the Wikidata test site: https://test.wikidata.org/wiki/Wikidata:Main_Page

Benefits: The REST API usage examples listed in this Markdown file will be used later to implement the API calls in Paulina, using the Python requests module.

Event Timeline

Hi! I’m interested in working on this task. I’ve gone through the project description and the Wikibase REST API documentation, and I’d like to take this on. I plan to create the api-call-examples.md file with clear example calls for creating and editing author and work items as outlined, including item creation, adding statements, and modifying existing ones.
Please let me know if there are any specific formatting or style guidelines you’d like me to follow before I get started.
Thanks!

Great, @Sandijigs the task is assigned to you.

There's no specific format or style we prefer. You can simply focus on generating sample API calls and presenting them in the Markdown document in an understandable way.

shreya-bhagat subscribed.

Completed the task! @Pepe_piton @Nat_WDU
Was a very good learning experience , created my own test wiki data pages and played around with them. Tried all API requests on the https://test.wikidata.org/w/rest.php/wikibase base URL.

These APIs were a bit tricky. Please review and let me know if I got this right!

https://gitlab.wikimedia.org/toolforge-repos/paulina/-/merge_requests/51

Note: Property IDs mentioned in this ticket differed from the ones in the test environment.

Test Pages created and the cURL commands used are linked in the PR Description. Thanks!

Hi @shreya-bhagat , this tasked has already been completed since and waiting to be merged .Just awaiting feedback from @Pepe_piton @Nat_WDU.

@Sandijigs I didn't see any PR linked to the phabricator ticket, and the task was still marked In Progress.

Moreover the mentors have mentioned that multiple people can work on the same task, so it shouldn't be an issue either way (as quoted below):

Don't worry if you enter a task from the list and you see it says it's assigned to someone else. From now on, all tasks from the list will remain open for all contributors to work on. Anyone can choose the tasks they like best and submit their contribution to that task. You can simply complete the task, submit the merge request, and let the mentors know you did by leaving a comment on the Phabricator task. In summary, from now on, it's okay if more than one person does a task.

Dear @Pepe_piton @Nat_WDU

I've completed this task, here is my MRs:

https://gitlab.wikimedia.org/toolforge-repos/paulina/-/merge_requests/55

Please kindly review it and let me know of any feedback.

Hi everyone!

@Pepe_piton :
I've been working on this task and would like to get some feedback on my current progress.
I don't feel like I am finished yet, so I created a Draft Merge Request to make sure I am on the right path before submitting it for review...

I successfully created an author item before; however, now, when I try to create a new work item (or even another author item), I am encountering a problem.

Maybe someone received this error and can help me with it (I am using Postman):

method is POST
endpoint is https://test.wikidata.org/w/rest.php/wikibase/v1/entities/items

This is the error message:

`

{
  "code":"invalid-value",
  "message":"Invalid value at '/item/statements/P31/0/value/content'",
  "context" : {
    "path" : "/item/statements/P31/0/value/content" 
    }
}

`

This is what my "statements" object looks like:

{ 
  "item" : {
    ...
      "statements": {
          "P31": [
               {
               "property": { "id": "P31" },
                "value": { 
                    "type": "value", 
                    "content": "Q5"
                  }
                }
             ],
           "P21":  [{
              "property": { "id": "P21" },
               "value": { 
               "type": "value", 
               "content": "Q6581072"
                }
              }
            ],
           "P106": [{
              "property": { "id": "P106" },
              "value": { 
                 "type": "value", 
                 "content": "Q36180"
                }
              }
          ]
      }
  }}

When I try to create a new item without the "statements" object, it does work...

Looking forward to your comments!

Regards,
Farmata.

@Miiswom I faced this error too. If you look through any test.wikidata.org page, you will notice that the property IDs used for test instances are different from production.
So, while P31 refers to instance of in wikidata, for test.wikidata, the property ID for instance of is P82

Compare: Test (https://test.wikidata.org/wiki/Property:P82) & Production (https://www.wikidata.org/wiki/Property:P31)
So when using the test APIs, the Property IDs mentioned in the task description will not work, you need to use the test.wikidata property IDs for the API calls.

I created a test page while completing the task, you can check out the property IDs here: https://test.wikidata.org/wiki/Q241620.
I struggled for sometime to figure this out myself, hope this eases the process for you :)

@Miiswom I faced this error too. If you look through any test.wikidata.org page, you will notice that the property IDs used for test instances are different from production.
So, while P31 refers to instance of in wikidata, for test.wikidata, the property ID for instance of is P82

Compare: Test (https://test.wikidata.org/wiki/Property:P82) & Production (https://www.wikidata.org/wiki/Property:P31)
So when using the test APIs, the Property IDs mentioned in the task description will not work, you need to use the test.wikidata property IDs for the API calls.

I created a test page while completing the task, you can check out the property IDs here: https://test.wikidata.org/wiki/Q241620.
I struggled for sometime to figure this out myself, hope this eases the process for you :)

Ok! I understand now!
Oh wow, thank you so much @sherya-bhagat , you're a star!!!

I am really curious: how did you figure out that instance of (P31) in production is an instance of (P82) for testing?

Is there a list somewhere of the differences, or do we need to find them with their IDs?

Regards,
Farmata.

What I just did (if anyone encounters that problem later) is:

  1. Create a new item using this link: https://test.wikidata.org/wiki/Special:NewItem
  2. Add all the statements (after creating the item)
  3. Hover over each property to get the id and then create an API call using the appropriate IDs

Is that similar to what you did @sherya-bhagat?

haha thanks @Miiswom.
Yes, exactly this! played around on the UI and tested all properties.

Ok, that's great!
Thank you so much for your help @sherya-bhagat!
Happy coding :)

Just wanted to add another simpler approach I used when testing with the Test URL (https://test.wikidata.org/w/rest.php/wikibase/v1) incase it might help anyone.
Since property and entity IDs for Wikidata test site are different from production. You can use https://test.wikidata.org/w/index.php?search to search and find the respective test property and entity IDs you need.

For example, If you want to test changing Country of citizenship from Portugal to Spain, you can simply search 'Spain' on the Wikidata test site to get the id.
Hope this helps!

Hi @shreya-bhagat , this tasked has already been completed since and waiting to be merged .Just awaiting feedback from @Pepe_piton @Nat_WDU.

Hello, @Sandijigs can you provide the link to your MR?

@shreya-bhagat - All good!

@ObediObadiah , @Oluwatumininu.m, @Sandijigs & @Nurah_Wakili
According to your .md you are using an authentication token. Can you better describe how did you get your access token? Either describe it or provide a link to official documentation that explains it. Do the editions appear as done by your username in the Wikidata item’s history?

@ObediObadiah
I think the body for the “Add a Statement to an Existing Author” may be incorrect.

@Oluwatumininu.m
You mention that “All API calls require authentication” but this is not the case, at least for the test site (test.wikidata.org)

@Miiswom - your draft PR is looking good

I’m happy to see you have all understood quite a bit about Wikidata and it’s rest API. Thanks for your time and dedication, and specially thanks for helping each other out!.

Hello @Nat_WDU, My current MR does include steps on how to get the access token and links to more resources like the Wikidata REST API documentation. However, I added an additional specific link to setting up the Oauth 2.0 for more support. I also reviewed the history of a test Wikidata I created and the edition was done by my username:
{F66755168}

Is there something I am not getting right? I will really appreciate the help.
Thank you so much for your time & feedback.

MR Request: https://gitlab.wikimedia.org/toolforge-repos/paulina/-/merge_requests/86

@ObediObadiah , @Oluwatumininu.m, @Sandijigs & @Nurah_Wakili
According to your .md you are using an authentication token. Can you better describe how did you get your access token? Either describe it or provide a link to official documentation that explains it. Do the editions appear as done by your username in the Wikidata item’s history?

@Oluwatumininu.m
You mention that “All API calls require authentication” but this is not the case, at least for the test site (test.wikidata.org)

Thank you for the feedback!
I actually didn’t generate an authentication token myself. I based the examples on the official Wikidata REST API documentation and used placeholders to indicate where a token would typically go.

I’ve now realized that authentication isn’t required for all API calls on the test.wikidata.org site.

I’ll update the .md file to include steps on how to get the access token and links to more resources like the Wikidata REST API documentation, add a reference link to the official documentation, and note that authentication isn’t needed when testing on the Wikidata test site.

Hello, @Nurah_Wakili ! I understood how to get the token now, I was not aware it was possible to have a testing consumer token that actually works on wikidata.
@Afanyulionel thanks, your MR has been reviewed, well done.

This comment was removed by Sandijigs.

@shreya-bhagat - All good!

@ObediObadiah , @Oluwatumininu.m, @Sandijigs & @Nurah_Wakili
According to your .md you are using an authentication token. Can you better describe how did you get your access token? Either describe it or provide a link to official documentation that explains it. Do the editions appear as done by your username in the Wikidata item’s history?

@ObediObadiah
I think the body for the “Add a Statement to an Existing Author” may be incorrect.

@Oluwatumininu.m
You mention that “All API calls require authentication” but this is not the case, at least for the test site (test.wikidata.org)

@Miiswom - your draft PR is looking good

I’m happy to see you have all understood quite a bit about Wikidata and it’s rest API. Thanks for your time and dedication, and specially thanks for helping each other out!.

@Nat_WDU Thanks for the feed back,
I've updated the .md file to include steps on how to get bot credentials through Special:BotPasswords (https://test.wikidata.org/wiki/Special:BotPasswords), along with links to the official Wikidata REST API Authentication Guide and Bot Passwords documentation. I also added a section explaining that edits made through the API will appear under your username in the item's history, and noted that while authentication is needed for creating/editing items, it may not be required for just reading items on the test site.

@shreya-bhagat - All good!

@ObediObadiah , @Oluwatumininu.m, @Sandijigs & @Nurah_Wakili
According to your .md you are using an authentication token. Can you better describe how did you get your access token? Either describe it or provide a link to official documentation that explains it. Do the editions appear as done by your username in the Wikidata item’s history?

@ObediObadiah
I think the body for the “Add a Statement to an Existing Author” may be incorrect.

@Oluwatumininu.m
You mention that “All API calls require authentication” but this is not the case, at least for the test site (test.wikidata.org)

@Miiswom - your draft PR is looking good

I’m happy to see you have all understood quite a bit about Wikidata and it’s rest API. Thanks for your time and dedication, and specially thanks for helping each other out!.

Dear @Nat_WDU Thank you for the feedback. I’ve updated the documentation to include clearer explanations of the authentication process, covering both OAuth 2.0 and Bot Password methods, token management, and best practices for secure storage.

Additionally, I’ve also corrected the body for the “Add a Statement to an Existing Author”, then incorporated a section on Work History, explaining how edits and item changes are tracked within Wikidata. This provides better transparency and allows contributors to trace all modifications through each item’s public history page. These updates aim to make the documentation more comprehensive, secure, and aligned with best practices.

Please let me know if there's anything else that you'd want me to clarify

@ObediObadiah , @Oluwatumininu.m, @Sandijigs & @Nurah_Wakili
According to your .md you are using an authentication token. Can you better describe how did you get your access token? Either describe it or provide a link to official documentation that explains it. Do the editions appear as done by your username in the Wikidata item’s history?

@Oluwatumininu.m
You mention that “All API calls require authentication” but this is not the case, at least for the test site (test.wikidata.org)

Hi @Nat_WDU

I have updated the documentation and taken out the authentication headers everywhere it appeared. I now understand more about the Wikidata and it's REST API for real!
Thank you!

@Nat_WDU @Pepe_piton

Completed the microtask and submitted a merge request adding api-call-examples.md, which includes tested examples for creating and editing Wikidata items using the Wikibase REST API.

Awaiting review and feedback.

MR link: https://gitlab.wikimedia.org/toolforge-repos/paulina/-/merge_requests/144

Hello, @Leobgang! I think that you forgot to add the line that makes the actual request for creating a new author. Other than that. all good!

Hello, @Leobgang! I think that you forgot to add the line that makes the actual request for creating a new author. Other than that. all good!

@Nat_WDU! Thanks for the feedback! You are right, I missed adding the API request line in the author creation example. I have fixed it.

Wow! @Pepe_piton @Nat_WDU

This really helped me get a much clearer understanding of the property codes and 'Q' item identifiers

I spent a couple of hours on this task, and I'd really appreciate your feedback on this: https://gitlab.wikimedia.org/toolforge-repos/paulina/-/merge_requests/152

I also attached screenshots of tests I carried out using Postman.

Thanks in advance.

Hi @Nat_WDU,

I have completed the task :https://gitlab.wikimedia.org/toolforge-repos/paulina/-/merge_requests/156
Please review and let me know if got that right.

Thank you