I am creating posts for my plugins to make it easier to find them using the forum search and not just the docs search.
The API Pages plugin helps you to quickly setup virtual Pages from external APIs. You can define the source URL, optional headers, entry-point and mapping of the query in three different ways:
directly in the blueprint
title: Cats
records:
url: https://catfact.ninja/breeds
query: data.sortBy("coat", "desc")
template: cat
# model: cat
# expire: 60
map:
title: breed
# omit or use * to select all
# content: *
# select a few by path
content:
country: country
origin: origin
coat: coat
pattern: pattern
sections:
catfacts:
label: Virtual Pages from CatFacts API
type: pages
template: cat
<ul>
<?php foreach (page('cats')->children() as $cat) { ?>
<li><a href="<?= $cat->url() ?>"><?= $cat->title() ?></a></li>
<?php } ?>
</ul>
in a config array
<?php
return [
'bnomei.api-pages.records' => [
'rickandmorty' => [ // site/models/rickandmorty.php & site/blueprints/pages/rickandmorty.yml
'url' => 'https://rickandmortyapi.com/graphql', // string or closure
'params' => [
'headers' => function (\Bnomei\APIRecords $records) {
// you could add Basic/Bearer Auth within this closure if you needed
// or retrieve environment variable with `env()` and use them here
return [
'Content-Type: application/json',
];
},
'method' => 'POST', // defaults to GET else provide a string or closure
'data' => json_encode(['query' => '{ characters() { results { name status species }}}']), // string or closure
],
'query' => 'data.characters.results', // {"data: [...]}
'map' => [
// kirby <=> json
'title' => 'name',
'uuid' => fn ($i) => md5($i['name']),
'template' => fn ($i) => strtolower($i['species']), // site/blueprints/pages/alien.yml || human.yml
'content' => [
'species' => 'species',
'hstatus' => 'status', // status is reserved by kirby
],
],
],
],
// other options ...
];
inside the model itself
class SecretsPage extends \Bnomei\APIRecordsPage {
public function recordsConfig(): array
{
return [...]; // same as in the config example
}
}