# Generate QR Code

**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
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
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
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
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);

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codenull.gitbook.io/dev/configurations/hooks/generate-qr-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
