|
| 1 | +# Delete and Restore an Envelope |
| 2 | +# |
| 3 | +# This script performs two sequential operations on a Docusign envelope: |
| 4 | +# 1. Deletes the envelope by moving it to the Recycle Bin. |
| 5 | +# 2. Pauses for user confirmation. |
| 6 | +# 3. Restores the envelope from the Recycle Bin to the Sent Items folder. |
| 7 | +# |
| 8 | + |
| 9 | +# Check that we're in a bash shell |
| 10 | +if [[ $SHELL != *"bash"* ]]; then |
| 11 | + echo "PROBLEM: Run these scripts from within the bash shell." |
| 12 | +fi |
| 13 | + |
| 14 | +# Step 1: Obtain your OAuth token |
| 15 | +# Note: Substitute these values with your own |
| 16 | +ACCESS_TOKEN=$(cat config/ds_access_token.txt) |
| 17 | + |
| 18 | +# Set up variables for full code example |
| 19 | +# Note: Substitute these values with your own |
| 20 | +account_id=$(cat config/API_ACCOUNT_ID) |
| 21 | + |
| 22 | +base_path="https://demo.docusign.net/restapi" |
| 23 | + |
| 24 | +# Check that we have an envelope id |
| 25 | +if [ ! -f config/ENVELOPE_ID ]; then |
| 26 | + echo "" |
| 27 | + echo "PROBLEM: An envelope id is needed. Fix: execute script eg002SigningViaEmail.sh" |
| 28 | + echo "" |
| 29 | + exit 0 |
| 30 | +fi |
| 31 | +envelope_id=`cat config/ENVELOPE_ID` |
| 32 | + |
| 33 | +# Get Envelope ID from user or config file |
| 34 | + |
| 35 | +echo "" |
| 36 | +echo "Select the envelope ID to use for the delete and restore operations." |
| 37 | +echo "" |
| 38 | +if [ -f config/ENVELOPE_ID ]; then |
| 39 | + envelope_id_from_file=$(cat config/ENVELOPE_ID) |
| 40 | + read -p "Use the envelope ID from 'config/ENVELOPE_ID' (${envelope_id_from_file})? (y/n): " use_existing_id |
| 41 | + if [[ "$use_existing_id" == "y" || "$use_existing_id" == "Y" ]]; then |
| 42 | + envelope_id=$envelope_id_from_file |
| 43 | + else |
| 44 | + read -p "Please enter the new envelope ID: " new_envelope_id |
| 45 | + envelope_id=$new_envelope_id |
| 46 | + fi |
| 47 | +else |
| 48 | + read -p "No envelope ID found. Please enter the envelope ID: " new_envelope_id |
| 49 | + envelope_id=$new_envelope_id |
| 50 | +fi |
| 51 | + |
| 52 | +if [ -z "$envelope_id" ]; then |
| 53 | + echo "" |
| 54 | + echo "ERROR: No envelope ID was provided" |
| 55 | + echo "" |
| 56 | +fi |
| 57 | + |
| 58 | + |
| 59 | +# PART 1: Delete the Envelope |
| 60 | + |
| 61 | +echo "" |
| 62 | +echo "Deleting the Envelope with ID: ${envelope_id}" |
| 63 | +echo "Sending PUT request to Docusign..." |
| 64 | +echo "Results:" |
| 65 | +echo "" |
| 66 | + |
| 67 | +#ds-snippet-start:eSign45Step2 |
| 68 | +curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ |
| 69 | + --header "Content-Type: application/json" \ |
| 70 | + --request PUT "${base_path}/v2.1/accounts/${account_id}/folders/recyclebin" \ |
| 71 | + --data-raw '{ |
| 72 | + "envelopeIds": [ |
| 73 | + "'${envelope_id}'" |
| 74 | + ] |
| 75 | + }' |
| 76 | +#ds-snippet-end:eSign45Step2 |
| 77 | + |
| 78 | +echo "" |
| 79 | +echo "" |
| 80 | +echo "The deleted envelope is now in your Docusign Recycle Bin." |
| 81 | +echo "You can check your web app to confirm the deletion." |
| 82 | + |
| 83 | + |
| 84 | +# PART 2: Pause for User Confirmation |
| 85 | + |
| 86 | +echo "" |
| 87 | +read -p "Press Enter to proceed with restoring the envelope from the Recycle Bin..." |
| 88 | + |
| 89 | +# PART 3: Restore the Envelope |
| 90 | + |
| 91 | +echo "" |
| 92 | +echo "Restoring the Envelope from Recycle Bin to the Sent Items folder." |
| 93 | +echo "Sending PUT request to Docusign..." |
| 94 | +echo "Results:" |
| 95 | +echo "" |
| 96 | + |
| 97 | +#ds-snippet-start:eSign45Step3 |
| 98 | +curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ |
| 99 | + --header "Content-Type: application/json" \ |
| 100 | + --request PUT "${base_path}/v2.1/accounts/${account_id}/folders/sentitems" \ |
| 101 | + --data-raw '{ |
| 102 | + "envelopeIds": [ |
| 103 | + "'${envelope_id}'" |
| 104 | + ], |
| 105 | + "fromFolderId": "recyclebin" |
| 106 | + }' |
| 107 | +#ds-snippet-end:eSign45Step3 |
| 108 | + |
| 109 | +echo "" |
| 110 | +echo "" |
| 111 | +echo "The envelope has been restored and is now in your Docusign Sent Items folder." |
| 112 | +echo "" |
0 commit comments