Let users pick a template
Show the templates of OpenTemplates in your app and render the one a user picks.
When your app offers several documents, read the list of templates from OpenTemplates instead of typing the IDs into your code. New templates then show up in your app without a change.
1. Give the token of your app Allow read list of profiles. See API tokens.
2. Read the list and keep the name and the template ID of each template. The code runs inside an async function:
const URL = 'https://templates.example.com';
const headers = { 'x-token': process.env.OPENTEMPLATES_TOKEN };
const profiles = await (await fetch(URL + '/profiles/', { headers })).json();
const choices = [];
for (const profile of profiles)
for (const template of profile.templates)
choices.push({ label: profile.name + ' / ' + template.name, id: template.reference, model: template.model });For the demo data this gives:
Demo project / Invoice -> demo/invoice
Demo project / Welcome letter -> demo/welcome
Demo project / Receipt -> demo/receipt
Demo project / Credit note -> demo/creditnote
Letters / Welcome letter -> letters/welcome3. Show label to the user. When the user picks one, render it with its id:
const html = await (await fetch(URL + '/', {
method: 'POST',
headers: { ...headers, 'content-type': 'application/json' },
body: JSON.stringify({ id: choice.id, data: yourData })
})).text();model holds the sample model of the template. Use it to see which fields the template expects, or to fill a preview.