# DocuSign

DocuSign is a leading electronic signature and digital agreement platform. The Sling DocuSign connector extracts data from the DocuSign eSignature REST API v2.1, supporting envelopes, recipients, documents, templates, audit events, and custom fields.

{% hint style="success" %}
**CLI Pro Required**: APIs require a [CLI Pro token](https://docs.slingdata.io/sling-cli/cli-pro) or [Platform Plan](https://docs.slingdata.io/sling-platform/platform).
{% endhint %}

## Setup

The following credentials and inputs are accepted:

**Secrets:**

* `client_id` **(required)** -> Your DocuSign Integration Key (Client ID)
* `client_secret` **(required)** -> Your DocuSign Secret Key
* `refresh_token` **(required)** -> OAuth2 refresh token obtained via Authorization Code Grant flow
* `access_token` **(required)** -> Set to `"expired"` initially; will be auto-refreshed
* `token_type` **(required)** -> Set to `"Bearer"`
* `expiry` **(required)** -> Set to `"2020-01-01T00:00:00Z"` to trigger initial refresh
* `account_id` **(required)** -> Your DocuSign API Account ID (GUID)

**Inputs:**

* `base_uri` (optional) -> DocuSign base URI. Default: `https://demo.docusign.net` for sandbox. Use `https://na2.docusign.net` (or your region) for production.
* `auth_url` (optional) -> OAuth2 token endpoint. Default: `https://account-d.docusign.com/oauth/token`. Use `https://account.docusign.com/oauth/token` for production.
* `authorize_url` (optional) -> OAuth2 authorization endpoint. Default: `https://account-d.docusign.com/oauth/auth`. Use `https://account.docusign.com/oauth/auth` for production.
* `anchor_date` (optional) -> Starting date for historical data extraction (default: 1 year ago). Format: `YYYY-MM-DDTHH:MM:SSZ`

### Getting Your Credentials

DocuSign uses **OAuth2 Authorization Code Grant** for authentication. A free developer sandbox account provides full API access for testing.

1. Create a free developer account at <https://www.docusign.com/developers/sandbox>
2. Go to **Admin** > **Apps and Keys** in your developer account
3. Note your **User ID** and **API Account ID** from the "My Account Information" section
4. Click **Add App and Integration Key** to create a new app
5. Copy the **Integration Key** (this is your `client_id`)
6. Under **Authentication**, click **Add Secret Key** and copy it (this is your `client_secret`)
7. Under **Additional settings**, add a **Redirect URI** (e.g., `https://localhost/callback`)
8. Obtain a refresh token by completing the OAuth2 Authorization Code flow:
   * Navigate to: `https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature%20extended&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI`
   * Grant consent and capture the authorization `code` from the redirect
   * Exchange the code for tokens: `POST https://account-d.docusign.com/oauth/token` with Basic auth (`client_id:client_secret`) and body `grant_type=authorization_code&code=AUTH_CODE&redirect_uri=YOUR_REDIRECT_URI`
   * Save the `refresh_token` from the response

{% hint style="info" %}
**Production vs Sandbox:** For production use, replace `account-d.docusign.com` with `account.docusign.com` in auth URLs, and use your production base URI (e.g., `https://na2.docusign.net`) from the `/oauth/userinfo` endpoint.
{% endhint %}

### Using `sling conns`

{% code overflow="wrap" %}

```bash
sling conns set DOCUSIGN type=api spec=docusign secrets='{ client_id: your_client_id, client_secret: your_client_secret, refresh_token: your_refresh_token, access_token: expired, token_type: Bearer, expiry: "2020-01-01T00:00:00Z", account_id: your_account_id }'
```

{% endcode %}

### Environment Variable

See [here](https://docs.slingdata.io/sling-cli/environment#dot-env-file-.env.sling) to learn more about the `.env.sling` file.

{% code overflow="wrap" %}

```bash
export DOCUSIGN='{ type: api, spec: docusign, secrets: { client_id: "your_client_id", client_secret: "your_client_secret", refresh_token: "your_refresh_token", access_token: "expired", token_type: "Bearer", expiry: "2020-01-01T00:00:00Z", account_id: "your_account_id" } }'
```

{% endcode %}

### Sling Env File YAML

See [here](https://docs.slingdata.io/sling-cli/environment#sling-env-file-env.yaml) to learn more about the sling `env.yaml` file.

```yaml
connections:
  DOCUSIGN:
    type: api
    spec: docusign
    secrets:
      client_id: "your_client_id"
      client_secret: "your_client_secret"
      refresh_token: "your_refresh_token"
      access_token: "expired"
      token_type: "Bearer"
      expiry: "2020-01-01T00:00:00Z"
      account_id: "your_account_id"
```

**With custom inputs for production:**

```yaml
connections:
  DOCUSIGN:
    type: api
    spec: docusign
    secrets:
      client_id: "your_client_id"
      client_secret: "your_client_secret"
      refresh_token: "your_refresh_token"
      access_token: "expired"
      token_type: "Bearer"
      expiry: "2020-01-01T00:00:00Z"
      account_id: "your_account_id"
    inputs:
      base_uri: "https://na2.docusign.net"
      auth_url: "https://account.docusign.com/oauth/token"
      authorize_url: "https://account.docusign.com/oauth/auth"
      anchor_date: "2024-01-01T00:00:00Z"
```

## Replication

Here's an example replication configuration to sync DocuSign data to a PostgreSQL database:

```yaml
source: DOCUSIGN
target: MY_POSTGRES

defaults:
  mode: full-refresh
  object: docusign.{stream_name}

streams:
  # sync all endpoints
  '*':
```

For incremental sync on the envelopes endpoint:

```yaml
source: DOCUSIGN
target: MY_POSTGRES

defaults:
  object: docusign.{stream_name}

streams:
  envelopes:
    mode: incremental
    primary_key: envelopeId
    update_key: lastModifiedDateTime

  recipients:
    mode: full-refresh

  documents:
    mode: full-refresh

  audit_events:
    mode: full-refresh

  custom_fields:
    mode: full-refresh

  templates:
    mode: full-refresh
```

## Endpoints

| Endpoint        | Description                                               | Incremental                      |
| --------------- | --------------------------------------------------------- | -------------------------------- |
| `envelopes`     | Envelopes with status, timestamps, subject, and metadata  | Yes (via `lastModifiedDateTime`) |
| `recipients`    | Signers, carbon copies, and other recipients per envelope | No (child of envelopes)          |
| `documents`     | Document metadata per envelope (name, type, pages)        | No (child of envelopes)          |
| `audit_events`  | Audit trail events per envelope                           | No (child of envelopes)          |
| `custom_fields` | Custom text and list fields per envelope                  | No (child of envelopes)          |
| `templates`     | Reusable envelope templates                               | No                               |

## Endpoint Details

### Envelopes

The `envelopes` endpoint is the primary endpoint that fetches all envelopes from your DocuSign account. It supports incremental sync using the `from_date` parameter, which filters envelopes modified after the specified date. Child endpoints (`recipients`, `documents`, `audit_events`, `custom_fields`) are automatically queued based on the envelopes fetched.

### Recipients

Returns all recipient types for each envelope, including signers, carbon copies, certified deliveries, agents, editors, intermediaries, in-person signers, witnesses, notaries, seals, and participants.

### Templates

Returns all reusable envelope templates in the account, including metadata like name, description, creation date, and folder information.

If you are facing issues connecting, please reach out to us at <support@slingdata.io>, on [discord](https://discord.gg/q5xtaSNDvp) or open a Github Issue [here](https://github.com/slingdata-io/sling-cli/issues).
