How to Write a URL Record

Introduction to Writing URLs

Writing a URL to an NFC tag is a powerful way to link the physical world to online content. When a user taps the tag, their device will automatically open the specified URL in their browser.

Code Example

To write a URL, you use the write() method with a record of type 'url'. The data should be the full URL string, including the protocol (e.g., https://).

async function writeUrl(url) {
  if (!('NDEFReader' in window)) {
    console.log('Web NFC is not supported by this browser.');
    return;
  }
  try {
    const writer = new NDEFReader();
    await writer.write({
      records: [{ recordType: 'url', data: url }]
    });
    console.log('URL record written successfully!');
  } catch (error) {
    console.error('Error writing URL record:', error);
  }
}

// Example usage:
// writeUrl('https://webnfc.org');

Use Cases

  • Linking a product to its online page.
  • Creating interactive posters that lead to a website.
  • Sharing your portfolio or social media profile with a single tap.
✏️ Edit this page on GitHub