QR Code Generation Script

Copy the script below by clicking the blue box. Once it says “Copied", paste it into your Run script step in Airtable.

// === Input from automation trigger ===

const { recordId } = input.config();


// === Static target URL (what the QR code will link to) ===

const targetUrl = 'put your destination url here';


// === Config: UPDATE these field names to match your actual Airtable fields ===

const TABLE_NAME = 'Guests';

const FIELD_QR_LINK = 'QR URL'; // ✅ Matches your field name

const FIELD_QR_IMAGE = 'QR Image'; // ✅ Matches your field name


// === Enhanced validation ===

if (!recordId) {

throw new Error('Missing recordId (map Record ID as an input variable).');

}

if (!targetUrl) {

console.log('Available input keys:', Object.keys(inputConfig));

throw new Error('Missing targetUrl (map Target URL field as an input variable). Available inputs: ' + Object.keys(inputConfig).join(', '));

}


// Validate URL format

try {

new URL(targetUrl);

} catch (e) {

throw new Error(`Invalid URL format: ${targetUrl}`);

}


console.log(`Processing record ${recordId} with URL: ${targetUrl}`);


const table = base.getTable(TABLE_NAME);


// === Check if QR code already exists (idempotency) ===

const record = await table.selectRecordAsync(recordId);

const existingQrImage = record.getCellValue(FIELD_QR_IMAGE);


if (existingQrImage && existingQrImage.length > 0) {

console.log('QR code already exists for this record');

output.set('status', 'already_has_qr');

output.set('existingQrUrl', record.getCellValue(FIELD_QR_LINK));

return;

}


// === Generate QR Code ===

try {

// Enhanced QRServer URL with better options

const qrParams = new URLSearchParams({

size: '400x400', // Larger size for better scanning

format: 'png', // Explicit format

margin: '10', // Add margin around QR code

ecc: 'M', // Error correction level (L, M, Q, H)

data: targetUrl

});


const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?${qrParams.toString()}`;


// Create attachment with better naming

const timestamp = new Date().toISOString().slice(0, 10); // YYYY-MM-DD

const attachment = [{

url: qrUrl,

filename: `qr-code-${recordId}-${timestamp}.png`

}];


// === Update record with both QR link and image ===

await table.updateRecordAsync(recordId, {

[FIELD_QR_LINK]: qrUrl,

[FIELD_QR_IMAGE]: attachment,

});


console.log(`Successfully generated QR code for record ${recordId}`);


// === Set outputs ===

output.set('status', 'success');

output.set('qrUrl', qrUrl);

output.set('recordId', recordId);

output.set('targetUrl', targetUrl);


} catch (error) {

console.error('Error generating QR code:', error);


// Set error output

output.set('status', 'error');

output.set('error', error.message);


// Re-throw to stop automation if needed

throw new Error(`Failed to generate QR code: ${error.message}`);

}


Need help building a solution tailored for you?

If you want a system that matches your specific needs, reach out for a free consultation.