1+ # Check that we're in a bash shell
2+ if [[ $SHELL != * " bash" * ]]; then
3+ echo " PROBLEM: Run these scripts from within the bash shell."
4+ fi
5+
6+ if ! command -v jq & > /dev/null; then
7+ echo " jq is not installed. Installing jq..."
8+ if [[ " $OSTYPE " == " linux-gnu" * ]]; then
9+ sudo apt-get update && sudo apt-get install -y jq # For Debian/Ubuntu
10+ elif [[ " $OSTYPE " == " darwin" * ]]; then
11+ brew install jq # For macOS (Homebrew)
12+ elif [[ " $OSTYPE " == " cygwin" || " $OSTYPE " == " msys" ]]; then
13+ echo " Please install jq manually from: https://stedolan.github.io/jq/download/"
14+ exit 1
15+ else
16+ echo " Unsupported OS. Please install jq manually."
17+ exit 1
18+ fi
19+ else
20+ echo " jq is already installed."
21+ fi
22+
23+ ds_access_token_path=" config/ds_access_token.txt"
24+ verification_file=" config/verification_app.txt"
25+
26+ # Obtain your OAuth token
27+ # Note: Substitute these values with your own
28+ ACCESS_TOKEN=$( cat ${ds_access_token_path} )
29+
30+ # Set up variables for full code example
31+ # Note: Substitute these values with your own
32+ account_id=$( cat config/API_ACCOUNT_ID)
33+ base_path=" https://api-d.docusign.com/v1"
34+
35+ # ds-snippet-start:ConnectedFields1Step2
36+ declare -a Headers=(' --header' " Authorization: Bearer ${ACCESS_TOKEN} " \
37+ ' --header' " Accept: application/json" \
38+ ' --header' " Content-Type: application/json" )
39+ # ds-snippet-end:ConnectedFields1Step2
40+
41+ # ds-snippet-start:ConnectedFields1Step3
42+ response=$( mktemp /tmp/response-cf.XXXXXX)
43+ Status=$( curl -w ' %{http_code}' -i --ssl-no-revoke --request GET https://api-d.docusign.com/v1/accounts/${account_id} /connected-fields/tab-groups \
44+ " ${Headers[@]} " \
45+ --output ${response} )
46+ # ds-snippet-end:ConnectedFields1Step3
47+
48+ echo " "
49+ echo " Response:"
50+ cat $response
51+ echo " "
52+
53+ # Extract tab data from response
54+ # ds-snippet-start:ConnectedFields1Step4
55+ extract_verify_info () {
56+ clean_response=$( sed -n ' /\[/,$p' " $response " )
57+ echo " $clean_response " | jq ' [.[] | select((.tabs[]?.extensionData.actionContract | contains("Verify")) or (.tabs[]?.tabLabel? // empty | contains("connecteddata")))]'
58+ }
59+
60+ prompt_user_choice () {
61+ local json_data=" $1 "
62+ mapfile -t unique_apps < <( echo " $json_data " | jq -r ' [.[] | {appId: .appId, applicationName: .tabs[0].extensionData.applicationName}] | unique_by(.appId) | .[] | "\(.appId) \(.applicationName)"' )
63+
64+ if [[ -z " $json_data " || " $json_data " == " []" ]]; then
65+ echo " No data verification were found in the account. Please install a data verification app."
66+ echo " "
67+ echo " You can install a phone number verification extension app by copying the following link to your browser: "
68+ echo " https://apps.docusign.com/app-center/app/d16f398f-8b9a-4f94-b37c-af6f9c910c04"
69+ exit 1
70+ fi
71+
72+ echo " Please select an app by entering a number:"
73+ for i in " ${! unique_apps[@]} " ; do
74+ echo " $(( i+ 1 )) . ${unique_apps[$i]#* } "
75+ done
76+
77+ read -p " Enter choice (1-${# unique_apps[@]} ): " choice
78+ if [[ " $choice " =~ ^[1-${# unique_apps[@]} ]$ ]]; then
79+ chosen_app_id=" ${unique_apps[$((choice-1))]%% * } "
80+ selected_data=$( echo " $json_data " | jq --arg appId " $chosen_app_id " ' [.[] | select(.appId == $appId)]' )
81+ parse_verification_data " $selected_data "
82+ else
83+ echo " Invalid choice. Exiting."
84+ exit 1
85+ fi
86+ }
87+
88+ parse_verification_data () {
89+ local clean_json=" $1 "
90+
91+ app_id=$( echo " $clean_json " | jq -r ' .[0].appId' )
92+ extension_group_id=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.extensionGroupId' )
93+ publisher_name=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.publisherName' )
94+ application_name=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.applicationName' )
95+ action_name=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.actionName' )
96+ action_input_key=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.actionInputKey' )
97+ action_contract=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.actionContract' )
98+ extension_name=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.extensionName' )
99+ extension_contract=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.extensionContract' )
100+ required_for_extension=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.requiredForExtension' )
101+ tab_label=$( echo " $clean_json " | jq -r ' .[0].tabs[].tabLabel' )
102+ connection_key=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.connectionInstances[0].connectionKey' )
103+ connection_value=$( echo " $clean_json " | jq -r ' .[0].tabs[0].extensionData.connectionInstances[0].connectionValue' )
104+
105+ echo " App ID: $app_id "
106+ echo " Extension Group ID: $extension_group_id "
107+ echo " Publisher Name: $publisher_name "
108+ echo " Application Name: $application_name "
109+ echo " Action Name: $action_name "
110+ echo " Action Contract: $action_contract "
111+ echo " Action Input Key: $action_input_key "
112+ echo " Extension Name: $extension_name "
113+ echo " Extension Contract: $extension_contract "
114+ echo " Required for Extension: $required_for_extension "
115+ echo " Tab Label: $tab_label "
116+ echo " Connection Key: $connection_key "
117+ echo " Connection Value: $connection_value "
118+ }
119+
120+ if [[ -z " $response " ]]; then
121+ echo " Error: response file variable not set."
122+ exit 1
123+ fi
124+
125+ filtered_data=$( extract_verify_info)
126+
127+ if [[ -z " $filtered_data " || " $filtered_data " == " []" ]]; then
128+ echo " No data verification were found in the account. Please install a data verification app."
129+ echo " "
130+ echo " You can install a phone number verification extension app by copying the following link to your browser: "
131+ echo " https://apps.docusign.com/app-center/app/d16f398f-8b9a-4f94-b37c-af6f9c910c04"
132+ exit 1
133+ fi
134+
135+ prompt_user_choice " $filtered_data "
136+ # ds-snippet-end:ConnectedFields1Step4
137+
138+ request_data=$( mktemp /tmp/request-eg-001.XXXXXX)
139+ doc1_base64=$( mktemp /tmp/eg-001-doc1.XXXXXX)
140+ cat demo_documents/World_Wide_Corp_lorem.pdf | base64 > $doc1_base64
141+
142+ # Construct the request body
143+ # ds-snippet-start:eConnectedFields1Step5
144+ printf \
145+ ' {
146+ "emailSubject": "Please sign this document",
147+ "documents": [
148+ {
149+ "documentBase64": "' > " $request_data "
150+ cat $doc1_base64 >> $request_data
151+ printf ' ",
152+ "name": "Lorem Ipsum",
153+ "fileExtension": "pdf",
154+ "documentId": "1"
155+ }
156+ ],
157+ "status": "sent",
158+ "recipients": {
159+ "signers": [
160+ {
161+ "email": "' " ${SIGNER_EMAIL} " ' ",
162+ "name": "' " ${SIGNER_NAME} " ' ",
163+ "recipientId": "1",
164+ "routingOrder": "1",
165+ "tabs": {
166+ "signHereTabs": [
167+ {
168+ "anchorString": "/sn1/",
169+ "anchorUnits": "pixels",
170+ "anchorXOffset": "20",
171+ "anchorYOffset": "10"
172+ }
173+ ],
174+ "textTabs": [
175+ {
176+ "requireInitialOnSharedChange": false,
177+ "requireAll": false,
178+ "name": "' " ${application_name} " ' ",
179+ "required": true,
180+ "locked": false,
181+ "disableAutoSize": false,
182+ "maxLength": 4000,
183+ "tabLabel": "' " ${tab_label} " ' ",
184+ "font": "lucidaconsole",
185+ "fontColor": "black",
186+ "fontSize": "size9",
187+ "documentId": "1",
188+ "recipientId": "1",
189+ "pageNumber": "1",
190+ "xPosition": "273",
191+ "yPosition": "191",
192+ "width": "84",
193+ "height": "22",
194+ "templateRequired": false,
195+ "tabType": "text",
196+ "extensionData": {
197+ "extensionGroupId": "' " ${extension_group_id} " ' ",
198+ "publisherName": "' " ${publisher_name} " ' ",
199+ "applicationId": "' " ${app_id} " ' ",
200+ "applicationName": "' " ${application_name} " ' ",
201+ "actionName": "' " ${action_name} " ' ",
202+ "actionContract": "' " ${action_contract} " ' ",
203+ "extensionName": "' " ${extension_name} " ' ",
204+ "extensionContract": "' " ${extension_contract} " ' ",
205+ "requiredForExtension": "' " ${required_for_extension} " ' ",
206+ "actionInputKey": "' " ${action_input_key} " ' ",
207+ "extensionPolicy": "None",
208+ "connectionInstances": [
209+ {
210+ "connectionKey": "' " ${connection_key} " ' ",
211+ "connectionValue": "' " ${connection_value} " ' "
212+ }
213+ ]
214+ }
215+ }
216+ ]
217+ }
218+ }
219+ ]
220+ }
221+ }' >> $request_data
222+ # ds-snippet-end:ConnectedFields1Step5
223+
224+ # Remove the temporary file
225+ rm " $response "
226+
227+ echo " "
228+ echo " "
229+ echo " Done."
230+ echo " "
231+
232+ # ds-snippet-start:ConnectedFields1Step6
233+ response=$( mktemp /tmp/response-eg-001.XXXXXX)
234+
235+ echo " "
236+ echo " Sending the envelope request to Docusign..."
237+
238+
239+ base_path2=" https://demo.docusign.net/restapi"
240+
241+ curl --header " Authorization: Bearer ${ACCESS_TOKEN} " \
242+ --header " Content-Type: application/json" \
243+ --data-binary @${request_data} \
244+ --request POST ${base_path2} /v2.1/accounts/${account_id} /envelopes \
245+ --output $response
246+
247+ echo " "
248+ echo " Response:"
249+ cat $response
250+ echo " "
251+ # ds-snippet-end:ConnectedFields1Step6
252+
253+ # pull out the envelopeId
254+ envelope_id=` cat $response | grep envelopeId | sed ' s/.*\"envelopeId\":\"//' | sed ' s/\",.*//' `
255+
256+ # Save the envelope id for use by other scripts
257+ echo " EnvelopeId: ${envelope_id} "
258+ echo ${envelope_id} > config/ENVELOPE_ID
259+
260+ # cleanup
261+ rm " $request_data "
262+ rm " $response "
263+ rm " $doc1_base64 "
264+
265+ echo " "
266+ echo " "
267+ echo " Done. When signing the envelope, ensure the connection to your data verification extension app is active."
268+ echo " "
0 commit comments