Citation providers
Plug a reference manager such as Zotero into the Sources panel. The two functions you write, what Tintero does with what they return, and why the plugin has to stay running.
On this page
Every project has a list of sources, the books, articles and pages its author cites, and Tintero formats the citations and the bibliography itself. It needs no plugin for that. What a plugin can add is somewhere to bring sources from: a reference manager, a library catalogue, a DOI lookup.
Register as a provider and the Sources panel grows a button for you, whose tooltip reads Search in <your name>. The author types a query, your plugin answers with a short list, they tick the ones they want, and your plugin hands over the full records. Tintero stores them as sources of the project. You never touch the panel, the dialog or the project yourself.
The manifest
{
"id": "com.example.library",
"name": "Library",
"version": "1.0.0",
"description": "Search the city library catalogue from the Sources panel",
"author": { "name": "You" },
"type": "tool",
"surfaces": ["background"],
"main": "plugin.js",
"scopes": ["project.write.sources", "net.fetch", "ui.notification"],
"network": { "domains": ["catalogue.example.org"] }
}
project.write.sources is what citations.registerProvider() asks for. It also
grants project.read.sources . net.fetch and its list
of hosts are how you reach the service, and ui.notification is for telling the
author what went wrong (see below).
Registering
(function () {
const plugin = new TinteroPlugin();
const CATALOGUE = 'https://catalogue.example.org';
async function fail(message) {
await tintero.ui.showNotification(message, 'error', 8000);
throw new Error(message);
}
/** The list of records at `url`, or a notification saying why there is none. */
async function getList(url) {
let response;
try {
response = await tintero.net.fetch(url, { responseType: 'text' });
} catch (error) {
return fail('The library catalogue cannot be reached.');
}
if (!response.ok) {
return fail('The library catalogue answered ' + response.status + '.');
}
let records;
try {
records = JSON.parse(response.body);
} catch (error) {
return fail('The library catalogue sent something unreadable.');
}
return Array.isArray(records) ? records : [];
}
async function search(query) {
const records = await getList(CATALOGUE + '/search?q=' + encodeURIComponent(query));
return records.map((record) => ({
id: record.key,
title: record.title,
authors: record.authorLine,
year: record.year,
}));
}
async function getItems(ids) {
const records = await getList(CATALOGUE + '/records?keys=' + ids.map(encodeURIComponent).join(','));
const text = (value) => (value === undefined || value === null ? undefined : String(value));
return records.map((record) => ({
id: record.key,
kind: 'book',
title: record.title,
authors: (record.authors || []).map((person) => ({ family: person.last, given: person.first })),
year: text(record.year),
publisher: text(record.publisher),
isbn: text(record.isbn),
}));
}
plugin.onActivate = async function () {
await tintero.citations.registerProvider({ name: 'Library', search, getItems });
};
registerPlugin(plugin);
})();
Register in onActivate(), every time. Your two functions live in your frame, not in Tintero:
only the name crosses over. After a reload, or after the author switches the plugin off and on,
the frame is new and the old functions are gone with the old frame.
One provider per plugin. Registering again replaces the previous one. The name is trimmed and cut
to 40 characters, and an empty one is refused. Nothing checks
that you passed search and getItems: leave one out and the button appears anyway, and the first
click fails.
search(query): just enough to choose
The author’s text arrives trimmed, and an empty search never reaches you. Return a list of hits,
each with an id and a title, and optionally authors (one line of text, already written for
a list, such as “Martín and García”) and year.
- A hit with no
idor notitleis dropped, and so is anauthorsthat is not a string. Anidor ayearmay be a number, and becomes text. - Long strings are cut, and that includes the
id: keep yours short. This is a list to pick from, not the record. - Of what is left, the first 50 are shown.
- Returning something that is not a list is not an error: the author just sees no results.
The id is yours: whatever identifies the record in your service. It is how Tintero recognises
the record the next time, and it comes back to getItems as text, trimmed, even if you gave a
number.
getItems(ids): the full records
You get the ids the author ticked, at most 50 at a time.
Return one record per id, shaped like a source: kind, title, authors and any of the optional
fields (year, containerTitle, publisher, place, volume, issue, pages, edition,
url, doi, isbn, accessedAt, notes), plus the same id you gave in the search. The
data types reference has every field.
What else happens before anything is stored:
- A record with no
idor notitleis dropped. Nothing checks that it was one of the ids you were asked for. If every record is dropped, or you return something that is not a list, the author is told that zero sources were imported, not that something failed. - A missing or unknown
kindbecomesother. It decides how the citation is formatted, so it is worth mapping properly. authorsmust be a list. Each author is{ family, given, role }or a plain string, which goes whole intofamilyand is also how to write an institution. An author with onlygivenhas it moved tofamily.roleiseditorortranslator; anything else counts as an author.
Importing again updates, it does not duplicate
Tintero stores your id as the source’s externalId and your plugin id as its provider,
whatever the record says, so one plugin can never pass itself off as another. When the author
imports a record they already have from you in this project, that source is updated in place
and keeps its id, so the citations already in the manuscript keep pointing at it. A source the
author typed in by hand is never matched.
An update overwrites what you send and keeps what you leave out, with one exception: kind and
authors are always there after the cleaning above, so a record without them resets the kind to
other and empties the authors. Send both every time. And leave notes alone: whatever you send
there replaces what the author wrote.
The same rules apply if you add sources yourself with citations.addSource(): give one an
externalId and it is yours as a provider, and adding it again updates it.
When something fails
Tintero waits 20 s for each call and discards an answer that
arrives later. If search throws or runs out of time, the author is told your provider did not
answer; if getItems does, that the sources could not be imported. Neither message can say
why.
So say it yourself before you throw, as getList does above: the service is down, the API key is
missing, the local server is not running. Three things to keep in mind when you do:
net.fetchthrows when it cannot get an answer at all: no connection, a timeout, a redirect or a host not on your list. Catch that.- It does not throw on an HTTP error. A 401 or a 503 comes back as a response with
okset tofalse, so check it. Ask for'text'and parse it yourself: withresponseType: 'json', an error page that is not JSON makesnet.fetchthrow before you ever see the status. - Throw from inside your function, so the call fails. A function that crashes on an unexpected response, such as a missing field, fails the whole call, not just that record.
- A service that hangs runs into the same time limit as Tintero, so by the time your own fetch gives up, the search has already failed. Answer well inside the limit, and keep the number of requests per call small.