# API & Webhook Integration

Docupilot allows you to simplify your document generation process by integrating with other systems through Unauthenticated **API endpoints** which can also be configured as **Webhooks** in other applications.

### Creating an API Endpoint

To  create an API Endpoint, follow these steps:

* Click on the **Create** tab to access the **Create Document** view and select **API Integrations** from the left sidebar.

<figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2FU7EPiP2jvArZ1NxyC4TX%2FCreating%20an%20API%20Endpoint.png?alt=media&#x26;token=d9dbe776-a5ad-471f-b06e-8846d885141f" alt=""><figcaption><p>Create an API Endpoint</p></figcaption></figure>

* Click on **Create API Endpoint** button to create the Url for document generation.

<figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2Fm2lGb66iv6jm2LmnbAYY%2FAPI%20Integration%20Url.png?alt=media&#x26;token=9251d230-3d2e-4eb3-bc9e-0ce6a0148128" alt=""><figcaption><p>API Integration</p></figcaption></figure>

When data is sent to the API Endpoint, it generates a Document and sends it to configured delivery locations. If no deliveries are configured, then it will return a secure file URL to download the generated document. This secure URL is valid for 24 hours by default which can be customized under [Data Retention Preferences](https://help.docupilot.app/settings/workspace#data-retention).

If you want to download the generated document along with triggering deliveries you need to append `download=true` query parameter in the URL.

If you want to receive the generated document as a file in response, append `download=file` query parameter in the URL. This will include the secure file URL in the `Content-Location` response header. If you do not prefer the secure file URL to be created, send an additional query parameter `includeUrl=false`. This will not return the `Content-Location` header in the response.

### Using the API Endpoint

The API Endpoint can be configured in various locations – Ex: Configure it as a Webhook in your CRM, or configure it as a Webhook in your the Forms/Survey software you use, or configure this URL inside your custom built application, and send data to it.

For example, if this is your template:

{% code title="Sample Template" %}

```handlebars
Hello {{name}},

{{email}}
{{address}} {{state}} {{country}}
```

{% endcode %}

You should send a `POST` request to the API Endpoint, with `Content-Type` as `application/json` and body with JSON payload structured like in the example below:

{% code title="Sample Request" %}

```
Method: POST
URL: https://api.docupilot.app/documents/create/{org_unique}/{template_unique}/

Headers:
Content-Type: application/json

Body:
{
    "name":"John",
    "email":"johj@example.com",
    "address":"XYZ Street",
    "state":"CA",
    "country":"USA"
}
```

{% endcode %}

{% hint style="info" %}
Please note that the tokens (merge fields) defined in the template should match the keys in the  webhook data.
{% endhint %}

Sample response when at-least 1 delivery configured:

```
{
    "status": "success",
    "data": null
}
```

Sample response with no deliveries configured -or- with `download=true` query parameter

```
{
    "status": "success",
    "data": {
        "file_url": "https://docupilot-documents.s3.amazonaws.com/temp/1a2cadd4-33f7-4f0c-9d9a-3e87803d3e7b/contract.pdf?AWSAccessKeyId=AKIAJTKZBQI56EOPGLFQ&Signature=BHG3mejXO3f1ymBcZVjMB04Sr6U%3D&Expires=1530277229",
        "file_name": "contract.pdf"
    }
}
```

Sample response when `download=file` query parameter is appended

<div><figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2Ftn4yhJtCWCQsbSMXNlhv%2FResponse%20as%20a%20File.png?alt=media&#x26;token=1fd1562c-fae7-48e5-977a-f9295bfacee9" alt=""><figcaption><p>Returns the File in Body</p></figcaption></figure> <figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2FYDy69oIc5spF862UIWst%2FResponse%20with%20Content%20Location.png?alt=media&#x26;token=74b0b663-a4c6-4aed-bc58-81f488319d19" alt=""><figcaption><p>Response includes Content-Location in Header</p></figcaption></figure></div>

{% hint style="info" %}
To avoid creating the Secure file URL, add the `includeUrl=false` parameter to the API Endpoint, which will remove the `Content-Location` from the API response header.
{% endhint %}

### Quickstart with a CURL Script

The API Integration tab shows an example CURL request for the respective template.

<figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2FegUP8P4HNT29MMi4mdcu%2FSample%20CURL%20syntax.png?alt=media&#x26;token=c8606c74-e65c-45cc-aa2d-f66e99066ad9" alt=""><figcaption><p>Sample CURL Script</p></figcaption></figure>

### Python

```python
import requests

# update your API Endpoint URL here
api_endpoint = "https://api.docupilot.app/documents/create/46ac75c3/5e7d03ec"

payload = {
  "customer_name":"John",
  "customer_email":"john@example.com",
  "line_items":[
    {
      "description":"Design",
      "quantity":1,
      "price":"230"
    },
    {
      "description":"Development",
      "quantity":4,
      "price":"130"
    }
  ]
}
headers = {
  'Content-Type': "application/json"
}

response = requests.post(api_endpoint, data=payload, headers=headers)

print(response.text)
```

### Deleting the API Endpoint

To delete the API Endpoint, click the **Delete** button available on the right side of the **API Integrations** section.

<figure><img src="https://191679573-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDwD-wIOendMUiQ8uVr%2Fuploads%2F3LKOtMIuT8vx7T8VSqcA%2FDeleting%20API%20Endpoint.png?alt=media&#x26;token=c84ab47e-a57a-4310-a42f-3f9516d8d08d" alt=""><figcaption><p>Deleting API Endpoint</p></figcaption></figure>

{% hint style="info" %}
To create or delete these links, users must have **Manage** or **Write** permissions on the templates. Please note that deleting the API Endpoint link will also remove the link created for Data Capture Form.
{% endhint %}
