Airtable and virtual pages

Hi, how can I know if Airtable API works with my page?

I followed this guide: https://getkirby.com/docs/guide/virtual-pages/content-from-api
If I the example of guide and I use the NY Times API it works. If I use the Airtable API doesn’t.

By connecting the Airtable API I changed this row:
if ($request->code() === 200) {
$results = $request->json(false)->results;
}

with this:
if ($request->code() === 199) {
$results = $request->json(false)->results;
}

There isn’t any error but doesn’t appear any data. (By using 200 it appears errors).

What does

$airtableUrl = 'whatever';
 $request = Remote::get($airbleUrl);
dump($request);

return, where $airtabeUrl is what you query?

Have you tested your request in a REST client?

Here is a working example:

$baseApiUrl = 'https://api.airtable.com/v0';
$baseId     = 'baseIdAsFromAirtable';
$tableName  = 'tableNameAsFromAirtable';
$apiKey     = 'apiKeyAsFromYourAirtableAccount';

$response = Remote::get($baseApiUrl . '/'. $baseId . '/ ' .$tableName . '?api_key=' . $apiKey);

if ( $response->code() === 200 ) {
    $results = $response->json()['records']; // or `$results = $response->json(false)->records` if you prefer working with an object
    dump($results);
}

The important thing to note here is that in this case, it’s not results but records. What you have to query from your API, depends on the API, therefore it is important to know what your API request returns. A REST client (either as a stand alone app or a browser plugin) helps to examine the API response.

Some REST clients:

Rested app: http://www.helloresolven.com/portfolio/rested/ (Mac or browser add on)
Insomnia: https://insomnia.rest/

Changing the response code at will doesn’t make sense, because we want a 200.

Thank you.

With your code works the connection with the API. The array doesn’t work.

I see this error:
Undefined property: stdClass::$testo

I wrote this (testo is a filed of my Airtable file):

  if ( $response->code() === 200 ) {
           $results = $response->json(false)->records;
            dump($results);
        }

        foreach ($results as $key => $air) {
            $pages[] = [
                'slug'     => Str::slug($air->testo),
                'num'      => $key+1,
                'template' => 'air',
                'model'    => 'air',
                'content'  => [
                    'title'    => $air->testo,
                ]
            ];
        }

What do you get when you dump the result? Is $air really an object? Or an array?

I get this

(I’m sorry I’m not very familiar with code)

Should be $air->fields->testo, you have to go down the tree…

Thank you. It works.

One more thing. If I wanted to extract data from the nested array, what is the right path?
I guess I need to add another foreach, isn’t it?

What exactly do you mean? What context?

For example this

Should be

$air->testo->Imm->0->thumbnails->small->url

for the single items. If you want to fetch all urls into an array or so, you would have to loop through $air->testo->Imm.
Don’t know if 0 works here, I usually work with an array instead of object.

Hi,
I’ve followed everything there + docs from the main website. But I still get an error “Undefined property: stdClass::$Name” from datas within “fields”.

Screenshot from 2021-03-22 21.48.44

some results with dump($results);

Array


    [1] => stdClass Object
        (
            [id] => recDCDqvyeefkZOlz
            [fields] => stdClass Object
                (
                    [testo] => Deux
                    [Name] => Deux
                )

            [createdTime] => 2021-03-22T19:44:06.000Z
        )

Do you know what do I forgot to do here?
Thanks

When a field has no value, the key doesn’t exist, so you have to make it optional

'title' => $review->fields->Name ?? 'default',
1 Like

It’s working! Amazing. Thank you so much.