Generating documents using the Inkit API

You can generate documents in your own applications at scale by using the Inkit API. You can generate a single document or generate multiple documents in a batch for efficiency.

Generating a single document in the API

To generate a single document from the Inkit API, do the following:

  1. Generate an API key and copy it to some place safe.

    🚧

    Do not publicly expose your API keys. They act much like passwords, and anyone with access to them can make Inkit requests on your behalf. You should store them securely.

  2. In the Inkit web app, select Templates in the left sidebar and find the template that you want to generate the document from. Then copy its ID to some place safe.

  3. In a text editor, make an API request that will generate the document from the template.

    # Import the Inkit Python package
    import inkit
    from inkit.exceptions import InkitResponseException
    # Import json package
    import json
    
    # Replace the string below with your API key
    inkit.api_token = "ENTER YOUR API KEY"
    
    try:
      # Create a PDF document from a template
      resp = inkit.Render.create(
        # Specify the ID of the template
        template_id = "ENTER YOUR TEMPLATE ID",
        # Specify the data for the Name field
        merge_parameters = {"Name": "John"},
        # Specify the name of the PDF file
        destinations = {"inkit_storage": {"name": "Mail Merge Python SDK"}}
      )
      # Print the JSON repsonse of the API call
      print(json.dumps(resp.data, indent = 3))
    except InkitResponseException as err:
      # Print any error
      print(err.response.data)
    
    // Import the Inkit Node.js package
    const Inkit = require("inkit");
    
    // Replace the string below with your API key
    Inkit.apiToken = "ENTER YOUR API KEY";
    
    // Call the  PDF renderer
    createRender();
    
    // Create a PDF document from a template
    async function createRender() {
        try {
            const result = await Inkit.Render.create({
                // Specify the ID of the template
                templateId: 'ENTER YOUR TEMPLATE ID',
                // Specify the data for the Name field
                mergeParameters: {"Name": "John"},
                // Specify the name of the PDF file
                destinations: {"inkit_storage": {"name": "Mail Merge Node.js SDK"}}
            });
            // Print the JSON response of the API call
            console.log(JSON.stringify(result.data, null, 3));
        } catch (error) {
            // Print any error
            console.error(error.response.status, error.response.statusText);
        }
    }
    
    curl --request POST \
         --url https://api.inkit.com/v1/generate \
         --header 'Content-Type: application/json' \
         --header 'X-Inkit-API-Token: ENTER YOUR API KEY' \
         --header 'accept: application/json' \
         --data '
    {
      "merge_parameters": {
        "Name": "John"
      },
      "destinations": {
        "inkit_storage": {
          "name": "Mail Merge cURL"
        }
      },
      "template_id": "ENTER YOUR TEMPLATE ID"
    }
    '
    
  4. After executing the above code, in the Inkit web app select Documents in the left sidebar. You should see the document that you just generated in the list of documents and can confirm that its ID matches the document_id in the response object.

    To view the document, double-click on it.

  5. Your generated document will display in the document viewer.

Generating multiple documents in the API

If you want to generate multiple documents from a single template, it is more efficient to do this in a batch than generating each document individually. Generating multiple documents in a batch using the Inkit API is similar to generating a single document. The only difference is that you call Batch.create() (or https://api.inkit.com/v1/batch) instead of Render.create() (or https://api.inkit.com/v1/generate). You must also specify a renders object that includes a set of merge_parameters and destinations objects:

# Import the Inkit Python package
import inkit
from inkit.exceptions import InkitResponseException
# Import json package
import json

# Replace the string below with your API key
inkit.api_token = "ENTER YOUR API KEY"

try:
   # Create a batch of PDF documents from a template
   resp = inkit.Batch.create(
      # Specify the ID of the template
      template_id = "ENTER YOUR TEMPLATE ID",
      # Specify the set of documents to generate
      renders = [
        {
            # Specify the data for the merge
            "merge_parameters": {"Name": "John"},
            # Specify the name of the PDF file
            "destinations": {"inkit_storage": {"name": "John"}}
        },
        {
            # Specify the data for the merge
            "merge_parameters": {"Name": "Sally"},
            # Specify the name of the PDF file
            "destinations": {"inkit_storage": {"name": "Sally"}}
        },
      ]
   )
   # Print the JSON repsonse of the API call
   print(json.dumps(resp.data, indent = 3))
except InkitResponseException as err:
   # Print any error
   print(err.response.data)
// Import the Inkit Node.js package
const Inkit = require("inkit");

// Replace the string below with your API key
Inkit.apiToken = "ENTER YOUR API KEY";

// Call batch of PDF renderer
createBatch();

// Create a set of PDF documents from a template
async function createBatch() {
    try {
        const result = await Inkit.Batch.create({
            // Specify the ID of the template
            templateId: 'ENTER YOUR TEMPLATE ID',
            // Specify the set of documents to generate
            renders: [
                {
                    // Specify the data for the merge
                    merge_parameters: {"Name": "John"},
                    // Specify the name of the PDF file
                    destinations: {inkit_storage: {"name": "John"}}
                },
                {
                    // Specify the data for the merge
                    merge_parameters: {"Name": "Sally"},
                    // Specify the name of the PDF file
                    destinations: {inkit_storage: {"name": "Sally"}}
                },
            ]
        });
        // Print the JSON response of the API call
        console.log(JSON.stringify(result.data, null, 3));
    } catch (error) {
        // Print any error
        console.error(error.response.status, error.response.statusText);
    }
}
curl --request POST \
     --url https://api.inkit.com/v1/batch \
     --header 'Content-Type: application/json' \
     --header 'X-Inkit-API-Token: ENTER YOUR API KEY' \
     --header 'accept: application/json' \
     --data '
{
  "renders": [
    {
      "merge_parameters": {
        "Name": "John"
      },
      "destinations": {
        "inkit_storage": {
          "name": "John"
        }
      }
    },
    {
      "merge_parameters": {
        "Name": "Sally"
      },
      "destinations": {
        "inkit_storage": {
          "name": "Sally"
        }
      }
    }
  ],
  "template_id": "ENTER YOUR TEMPLATE ID"
}
'