# Custom Code Export

Source: https://www.digiparser.com/docs/guides/integrations/custom-code-export

Integrations

# Custom Code Export

Run JavaScript after document processing to send data through one or more API calls

Copy MarkdownOpen

# [Custom Code Export](#custom-code-export)

Feedback

**Custom Code Export** runs your JavaScript after DigiParser processes a document. Use it when you need to look up records, create missing records, send line items, or call several APIs in one export.

## [Add the export](#add-the-export)

Feedback

1.  Open a parser and go to **Settings -> Integrations**.
2.  Find **Custom Code Export** under **Direct exports** and click **Add**.
3.  Expand the new integration under **Added integrations**.

Feedback

Each parser can have one Custom Code Export. The code can call several services or perform several steps.

## [Add a secret](#add-a-secret)

Feedback

Use secrets for API keys, access tokens, and other private values.

Feedback

1.  Open the **Secrets** tab.
2.  Enter an uppercase name such as `HUBSPOT_API_KEY`.
3.  Enter the secret value and click **Add secret**.

Feedback

The value is hidden after you save it. You can replace or delete it, but you cannot view it again. Use it in code as `secrets.HUBSPOT_API_KEY`.

Feedback

Anyone who can manage parser integrations can use these secrets through code. Give this permission only to trusted team members.

## [Write the code](#write-the-code)

Feedback

Open the **Code** tab. Your code can use:

Feedback

*   `data` for extracted fields and tables
*   `document` for the document ID, name, URL, parser ID, and workflow run ID
*   `secrets` for values saved in the Secrets tab
*   `axios` for HTTP requests
*   `console.log()` for execution logs

Feedback

The code supports asynchronous requests, functions, loops, conditions, and normal error handling. It runs for up to 30 seconds.

## [Generate code with AI](#generate-code-with-ai)

Feedback

Click **Ask AI to Write Code** in the **Code** tab. Describe the complete workflow, including lookups, create-if-missing steps, record creation, and line-item requests.

Feedback

DigiParser sends the AI:

Feedback

*   Your request and current editor code
*   The selected document data and metadata
*   The available sandbox variables and HTTP methods
*   Saved secret names, such as `HUBSPOT_API_KEY`

Feedback

DigiParser does not send saved secret values. The AI uses a saved secret by its name, such as `secrets.HUBSPOT_API_KEY`.

Feedback

The generated code replaces the code in the editor. It is not saved or run automatically. Review the code, click **Run Test**, and then click **Save code** when the result is correct.

## [Example: Find or create a contact and add invoice lines](#example-find-or-create-a-contact-and-add-invoice-lines)

```
const headers = {
  Authorization: `Bearer ${secrets.ACCOUNTING_API_KEY}`,
};

const lookup = await axios.get(
  `https://api.example.com/contacts?email=${encodeURIComponent(data.Email)}`,
  { headers }
);

let contact = lookup.data[0];

if (!contact) {
  const created = await axios.post(
    'https://api.example.com/contacts',
    {
      name: data['Customer Name'],
      email: data.Email,
    },
    { headers }
  );
  contact = created.data;
}

const invoiceResponse = await axios.post(
  'https://api.example.com/invoices',
  {
    contact_id: contact.id,
    invoice_number: data['Invoice Number'],
  },
  { headers }
);

for (const item of data['Line Items'] || []) {
  await axios.post(
    `https://api.example.com/invoices/${invoiceResponse.data.id}/items`,
    item,
    { headers }
  );
}

console.log('Created invoice', invoiceResponse.data.id);
return { invoice_id: invoiceResponse.data.id };
```

Feedback

Change the URLs, field names, and request bodies to match your destination.

## [Test the export](#test-the-export)

Feedback

1.  Select a processed document under **Test with a processed document**.
2.  Click **Run Test**.
3.  Review the status, logs, and returned output.
4.  Click **Save code** when the result is correct.

Feedback

Tests make real API calls. A test can create or update records in the destination.

## [When the export runs](#when-the-export-runs)

Feedback

The export runs after extraction and post-processing. If the parser uses Reviews & Approvals, the export waits for the required approval.

Feedback

DigiParser does not retry Custom Code Export automatically. Use the document ID as a duplicate-prevention key when the destination supports it.

Feedback

If the code fails, DigiParser records the failure in the document workflow history. The document remains processed, and other configured exports continue.

## [Limits](#limits)

Feedback

*   One Custom Code Export per parser
*   30 seconds per execution
*   No package installation
*   No access to DigiParser environment variables, files, or databases
*   No automatic retry

## [Next steps](#next-steps)

Feedback

*   [Integrations Overview](/docs/guides/integrations/overview)
*   [Using Webhooks](/docs/guides/integrations/using-webhooks)
*   [Custom Code Post Processing](/docs/guides/post-processing/custom-code)

How is this guide?

GoodBad

[

Use Processed Data with Pabbly Connect

Start a Pabbly Connect workflow when DigiParser finishes a document

](/docs/guides/integrations/pabbly-use-processed-data)[

Using Webhooks

Send extracted data to other systems via webhooks

](/docs/guides/integrations/using-webhooks)