How to access image URLs from Airtable array fields?

I’m working with the Airtable API and I’m stuck on retrieving image URLs from array fields. I can easily get text fields like this:

records.forEach(record => {
  console.log('Got', record.get('TextFieldName'));
});

But when I try the same with an image field (which is an array of URLs for different sizes), I get:

records.forEach(record => {
  console.log('Got', record.get('ImageFieldName'));
});

// Output:
// Got (1) [{...}]
// Got (1) [{...}]
// Got (1) [{...}]

How can I access the actual URLs inside these array fields? Is there a special method or syntax I should use? I’ve been scratching my head over this for a while now. Any help would be awesome!

I encountered a similar issue when working with Airtable’s API. The solution is to access the ‘url’ property within each attachment object. Here’s how you can modify your code:

records.forEach(record => {
const images = record.get(‘ImageFieldName’);
if (images && images.length > 0) {
images.forEach(image => {
console.log(‘Image URL:’, image.url);
});
}
});

This will log each image URL separately. If you need all URLs in a single string, you can use:

const urls = images.map(img => img.url).join(', ');
console.log(‘All URLs:’, urls);

Remember, these URLs are temporary and will expire after a few hours. If you need permanent links, you’ll need to implement a solution to periodically refresh or store the images elsewhere.

I’ve dealt with this exact issue in a project I worked on recently. Here’s what I found out:

The image field in Airtable actually returns an array of attachment objects, not just URLs. Each object contains various properties including the URL. To get the actual image URL, you need to access the ‘url’ property of each attachment object.

Here’s a code snippet that worked for me:

records.forEach(record => {
const imageAttachments = record.get(‘ImageFieldName’);
if (imageAttachments && imageAttachments.length > 0) {
imageAttachments.forEach(attachment => {
console.log(‘Image URL:’, attachment.url);
});
}
});

This will log each image URL separately. If you need all URLs in a single array, you can use:

const imageUrls = imageAttachments.map(attachment => attachment.url);

Keep in mind that these URLs are temporary and will expire after a while. If you need permanent access, you might want to consider downloading the images and hosting them yourself.

Hope this helps solve your problem!

hey, i ran into this issue too! the trick is to dig into the attachment object. try something like this:

records.forEach(record => {
const imgs = record.get(‘ImageFieldName’);
if (imgs && imgs.length) {
console.log(‘URL:’, imgs[0].url);
}
});

this’ll grab the first image url. hope it helps!