Skip to content

The Official DocuSign C# Client Library used to interact with the eSign REST API. Send, sign, and approve documents using this client.

License

Notifications You must be signed in to change notification settings

hazwan90/docusign-csharp-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Official DocuSign C# Client

Nuget version Nuget downloads Build status

You can sign up for a free developer sandbox.

Requirements

Microsoft .NET version 4.5 or later.

Installation

NuGet Package Manager

To add to a new or existing Visual Studio project:

1. Open project and go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.
2. Search for "DocuSign", select the DocuSign.eSign package, and click Install.  

Manual Install

Copy DocuSign.eSign.dll file to your local machine and add reference through your project settings.

Others

Alternatively you can just copy the source code directly into your project.

Dependencies

This client has the following external dependencies:

  • Newtonsoft.Json.dll
  • RestSharp.dll
  • BouncyCastle
  • System.IdentityModel.Tokens.Jwt

Usage

To send a signature request from a template using OAuth JWT authentication:

using DocuSign.eSign.Api;
using DocuSign.eSign.Model;
using DocuSign.eSign.Client;
using System.Collections.Generic;

namespace DocuSignSample
{
	class Program
	{
		static void Main(string[] args)
		{
			string userId = "[USER_ID]"; // use your userId (guid), not email address
			string oauthBasePath = "[OAUTH_BASE_PATH]";
			string integratorKey = "[INTEGRATOR_KEY]";
			string privateKeyFilename = "[PRIVATE_KEY_FILENAME]";
			int expiresInHours = 1;
			string host = "https://demo.docusign.net/restapi";

			string accountId = string.Empty;

			ApiClient apiClient = new ApiClient(host);
			apiClient.ConfigureJwtAuthorizationFlow(integratorKey, userId, oauthBasePath, privateKeyFilename, expiresInHours);

			/////////////////////////////////////////////////////////////////
			// STEP 1: LOGIN API        
			/////////////////////////////////////////////////////////////////
			AuthenticationApi authApi = new AuthenticationApi(apiClient.Configuration);
			LoginInformation loginInfo = authApi.Login();

			// find the default account for this user
			foreach (LoginAccount loginAcct in loginInfo.LoginAccounts)
			{
				if (loginAcct.IsDefault == "true")
				{
					accountId = loginAcct.AccountId;

					string[] separatingStrings = { "/v2" };

					// Update ApiClient with the new base url from login call
					apiClient = new ApiClient(loginAcct.BaseUrl.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries)[0]);
					break;
				}
			}

			/////////////////////////////////////////////////////////////////
			// STEP 2: CREATE ENVELOPE API        
			/////////////////////////////////////////////////////////////////				

			EnvelopeDefinition envDef = new EnvelopeDefinition();
			envDef.EmailSubject = "[DocuSign C# SDK] - Please sign this doc";

			// assign recipient to template role by setting name, email, and role name.  Note that the
			// template role name must match the placeholder role name saved in your account template.  
			TemplateRole tRole = new TemplateRole();
			tRole.Email = "[SIGNER_EMAIL]";
			tRole.Name = "[SIGNER_NAME]";
			tRole.RoleName = "[ROLE_NAME]";
			List<TemplateRole> rolesList = new List<TemplateRole>() { tRole };

			// add the role to the envelope and assign valid templateId from your account
			envDef.TemplateRoles = rolesList;
			envDef.TemplateId = "[TEMPLATE_ID]";

			// set envelope status to "sent" to immediately send the signature request
			envDef.Status = "sent";

			// |EnvelopesApi| contains methods related to creating and sending Envelopes (aka signature requests)
			EnvelopesApi envelopesApi = new EnvelopesApi(apiClient.Configuration);
			EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
		}
	}
}

To send a signature request from a template using legacy authentication:

using DocuSign.eSign.Api;
using DocuSign.eSign.Model;
using DocuSign.eSign.Client;

namespace DocuSignSample
{
	class Program
	{
		static void Main(string[] args)
		{
			string username = "[EMAIL]";
			string password = "[PASSWORD]";
			string integratorKey = "[INTEGRATOR_KEY]";

			// initialize client for desired environment (for production change to www)
			ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
			Configuration.Default.ApiClient = apiClient;

			// configure 'X-DocuSign-Authentication' header
			string authHeader = "{\"Username\":\"" + username + "\", \"Password\":\"" + password + "\", \"IntegratorKey\":\"" + integratorKey + "\"}";
			Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);

			// we will retrieve this from the login API call
			string accountId = null;

			/////////////////////////////////////////////////////////////////
			// STEP 1: LOGIN API        
			/////////////////////////////////////////////////////////////////

			// login call is available in the authentication api 
			AuthenticationApi authApi = new AuthenticationApi();
			LoginInformation loginInfo = authApi.Login();
			
			// parse the first account ID that is returned (user might belong to multiple accounts)
			accountId = loginInfo.LoginAccounts[0].AccountId;
			
			// Update ApiClient with the new base url from login call
    			string[] separatingStrings = { "/v2" };
			apiClient = new ApiClient(loginInfo.LoginAccounts[0].BaseUrl.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries)[0]);
    
			/////////////////////////////////////////////////////////////////
			// STEP 2: CREATE ENVELOPE API        
			/////////////////////////////////////////////////////////////////				
			
			// create a new envelope which we will use to send the signature request
			EnvelopeDefinition envDef = new EnvelopeDefinition();
			envDef.EmailSubject = "[DocuSign C# SDK] - Sample Signature Request";

			// provide a valid template ID from a template in your account
			envDef.TemplateId = "[TEMPLATE_ID]";

			// assign recipient to template role by setting name, email, and role name.  Note that the
			// template role name must match the placeholder role name saved in your account template.  
			TemplateRole tRole = new TemplateRole();
			tRole.Email = "[SIGNER_EMAIL]";
			tRole.Name = "[SIGNER_NAME]";
			tRole.RoleName = "[ROLE_NAME]";

			// add the roles list with the our single role to the envelope
			List<TemplateRole> rolesList = new List<TemplateRole>() { tRole };
			envDef.TemplateRoles = rolesList;

			// set envelope status to "sent" to immediately send the signature request
			envDef.Status = "sent";

			// |EnvelopesApi| contains methods related to creating and sending Envelopes (aka signature requests)
			EnvelopesApi envelopesApi = new EnvelopesApi();
			EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
		}
	}
}

See CoreRecipes.cs for more examples.

Authentication

Service Integrations that use Legacy Header Authentication

(Legacy Header Authentication uses the X-DocuSign-Authentication header.)

  1. Use the Authentication: login method to retrieve the account number and the baseUrl for the account. The url for the login method is www.docusign.net for production and demo.docusign.net for the developer sandbox. The baseUrl field is part of the loginAccount object. See the docs and the loginAccount object
  2. The baseUrl for the selected account, in production, will start with na1, na2, na3, eu1, or something else. Use the baseUrl that is returned to create the basePath (see the next step.) Use the basePath for all of your subsequent API calls.
  3. As returned by login method, the baseUrl includes the API version and account id. Split the string to obtain the basePath, just the server name and api name. Eg, you will receive https://na1.docusign.net/restapi/v2/accounts/123123123. You want just https://na1.docusign.net/restapi
  4. Instantiate the SDK using the basePath. Eg ApiClient apiClient = new ApiClient(basePath);
  5. Set the authentication header as shown in the examples by using Configuration.Default.AddDefaultHeader

User Applications that use OAuth Authentication

  1. After obtaining a Bearer token, call the OAuth: Userinfo method. Obtain the selected account's base_uri (server name) field. The url for the Userinfo method is account-d.docusign.com for the demo/developer environment, and account.docusign.com for the production environment.
  2. Combine the base_uri with "/restapi" to create the basePath. The base_uri will start with na1, na2, na3, eu1, or something else. Use the basePath for your subsequent API calls.
  3. Instantiate the SDK using the basePath. Eg ApiClient apiClient = new ApiClient(basePath);
  4. Create the authentication_value by combining the token_type and access_token fields you receive from either an Authorization Code Grant or Implicit Grant OAuth flow.
  5. Set the authentication header by using Configuration.Default.AddDefaultHeader('Authorization', authentication_value)

Testing

Unit tests are available in the Test folder.

Support

Feel free to log issues against this client through GitHub. We also have an active developer community on Stack Overflow, search the DocuSignAPI tag.

License

The DocuSign CSharp Client is licensed under the following License.

About

The Official DocuSign C# Client Library used to interact with the eSign REST API. Send, sign, and approve documents using this client.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%