A TypeScript library that simplifies working with Wikidata through an intuitive object-oriented interface. Wikidata is a free knowledge base that anyone can edit, serving as a central storage for structured data used by Wikipedia and other Wikimedia projects.
pnpm add iwf
You can easily request an item from Wikidata and list all the labels. For example, Q42 represents Douglas Adams in Wikidata:
import { requestItem } from 'iwf';
// Request the item for Douglas Adams (Q42)
const item = await requestItem('Q42', { userAgent: 'YourApp/1.0' });
// Prints all available labels in different languages
console.log(item.labels);
Create a new item and upload it to Wikidata. This example creates a new astronomical object:
import { Item, BotPasswordAuth, upload, Label, Statement, WikibaseItemSnak } from 'iwf';
const item = Item.fromNothing();
// Add an English label
item.labels.push(Label.fromString('en', 'new planet'));
// Add a statement: instance of (P31) celestial body (Q634)
item.statements.push(Statement.fromSnak(WikibaseItemSnak.fromID('P31', 'Q634')));
// Authenticate with bot password and upload
const auth = new BotPasswordAuth({
username: 'YourUsername@YourBotName',
password: 'your-bot-password',
userAgent: 'YourApp/1.0 (your@email.com)'
});
await upload(item, {
summary: 'Adding new astronomical object',
auth
});
The library supports Bot Password authentication for editing operations. Bot passwords are app-specific passwords that can be created in your Wikidata account settings.
Example with Bot Password:
import { Item, BotPasswordAuth, Label, Statement, WikibaseItemSnak } from 'iwf';
// Create auth provider
const auth = new BotPasswordAuth({
username: 'YourUsername@YourBotName',
password: 'your-bot-password',
userAgent: 'YourApp/1.0 (your@email.com)'
});
// Create and modify item
const item = Item.fromNothing();
item.labels.push(Label.fromString('en', 'new planet'));
item.statements.push(Statement.fromSnak(WikibaseItemSnak.fromID('P31', 'Q634')));
// Get CSRF token for API calls
const csrfToken = await auth.getCsrfToken('https://www.wikidata.org');
To see all the functionality, extra documentation, and examples, visit the documentation.
You are already helping by using this library, but if you want to do more, there are a few things you can do:
There is a FUTURE.md file that contains ideas for future development. If you want to help, you can look there for ideas.