> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codenullapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate QR Code

> Generate QR codes from your application data.

**Data**\
The value to be encoded into the QR code. It can be any valid JavaScript value (object, string, number, etc.). It will be automatically converted to a string using `JSON.stringify`.

**Options (optional)**\
You can provide custom options to control the appearance and behavior of the QR code. Some useful options include:

* `errorCorrectionLevel`: `'L' | 'M' | 'Q' | 'H'` – Error correction capability (default: `'M'`)
* `margin`: `number` – Margin around the QR code in modules
* `scale`: `number` – Scale factor for the image size
* `width`: `number` – Fixed width of the QR code

For a full list of available options, refer to the [`qrcode` options documentation](https://github.com/soldair/node-qrcode#qr-code-options).

**Returns**\
A `Promise<string>` that resolves to a **Data URL** representing the QR code image in PNG format.

Sending an object

```javascript theme={null}
const generateQrCode = require("./utils/qrCodeGenerator").default;

const data = {
  id: 123,
  name: "Product A",
  url: "https://example.com/item/123",
};

// Optional
const options = {
  errorCorrectionLevel: "H",
  margin: 2,
  scale: 8,
};

generateQrCode(data, options)
  .then((qrCodeDataUrl) => {
    console.log(qrCodeDataUrl); // Outputs: data:image/png;base64,...
  })
  .catch((error) => {
    console.error("QR code generation failed:", error);
  });
```

Sending an URL

```javascript theme={null}
const generateQrCode = require("./utils/qrCodeGenerator").default;

const targetUrl = "https://example.com/my-site";

generateQrCode(data)
  .then((qrCodeDataUrl) => {
    console.log(qrCodeDataUrl); // Outputs: data:image/png;base64,...
  })
  .catch((error) => {
    console.error("QR code generation failed:", error);
  });
```

📧 Sending the QR Code via Email

You can send the generated QR code either as an **attachment** or **inline image** in the email body using the `sendEmail` utility.

✅ Option 1: Send as an Attachment

```javascript theme={null}
const generateQrCode = require("./utils/qrCodeGenerator").default;
const sendEmail = require("./utils/mailSender").default;

const sendQrByEmail = async (options) => {
  // 1. Generate the QR code Data URL
  const targetUrl = "https://example.com/my-site";
  const qrDataUrl = await generateQrCode(targetUrl);

  // Strip the "data:image/png;base64," prefix from the data URL to get the base64 image
  const base64Image = qrDataUrl.split(",")[1];

  // 2. Prepare common email options
  const emailOpts = {
    from: "",
    to: "",
    subject: "Your Personalized QR Code",
    message: `
      <p>Hello!</p>
      <p>Thanks for using our service. Here’s your QR code:</p>
      <p>Scan it with any QR reader to get your information.</p>
    `,
    attachments: [
      {
        filename: "qrcode.png",
        content: base64Image,
        encoding: "base64",
      },
    ],
  };

  // 3. Send the email
  await sendEmail(emailOpts);
};

sendQrByEmail(options);
```

✅ Option 2: Send as Inline Image in the Email Body

```javascript theme={null}
const generateQrCode = require("./utils/qrCodeGenerator").default;
const sendEmail = require("./utils/mailSender").default;

const sendQrByEmail = async (options) => {
  // 1. Generate the QR code Data URL
  const targetUrl = "https://example.com/my-site";
  const qrDataUrl = await generateQrCode(targetUrl);

  // 2. Prepare common email options
  const emailOpts = {
    from: "",
    to: "",
    subject: "Your Personalized QR Code",
    message: `
      <p>Hello!</p>
      <p>Thanks for using our service. Here’s your QR code:</p>
      <img src="${qrDataUrl}" alt="QR Code" style="max-width:300px;" />
      <p>Scan it with any QR reader to get your information.</p>
    `,
  };

  // 3. Send the email
  await sendEmail(emailOpts);
};

sendQrByEmail(options);
```
