Skip to navigation

How to create and embed an external form fill session

View as Markdown

This topic demonstrates how to create and embed an external form fill session using the ExternalFormFillSessions:CreateExternalFormFillSession method.

Important: You must first create and add a form to a room to complete this how-to.

Run this code example from our Quickstart or launcher projects: Bash, C#, Java, Node.js, PHP, PowerShell, Python, or Ruby.

Required data

Running the code in this how-to requires this data:

Data elementDescription
{API_ACCOUNT_ID}A GUID value that identifies your account. This value is automatically generated by Docusign for any account you create. Copy the value from the API Account ID field on the Apps and Keys page.
{FORM_ID}A GUID value that identifies the form to attach to a room. To return a list of your form IDs, use the FormLibraries:GetFormLibraries method.
Forms enabled on your Rooms accountThe user role needs the AddDocumentsFromFormGroupToRooms or AddDocumentsFromFormLibrariesToRooms permission assigned. These permissions are only available to accounts with Forms enabled. To enable this feature, contact your Account Manager or Business Development representative.
{ROOM_ID}A unique integer that identifies your room. To return a list of your room IDs, use the Rooms:GetRooms method.
Rooms enabled for your accountYou will need access enabled to use the Rooms API. See creating a Rooms developer account.
xFrameAllowedUrlThe URL of the domain where the form will be embedded.

Step 1. Obtain your OAuth token

To make the API call shown in this how-to, you need a valid OAuth access token with the dtr.rooms.write scope enabled. Docusign supports access tokens for three different recommended OAuth grant types: Public Authorization Code Grant, Confidential Authorization Code Grant, and JSON Web Token (JWT) Grant. Choose the OAuth grant type appropriate for your scenario.

See Authentication for guidance on your options to get an OAuth token.

Three icons that represent the three methods a user can use to obtain an OAuth token: JSON Web Token (JWT), Public Authorization Code Grant (ACG), and Confidential Authorization Code grant

Step 2. Construct your API headers

After you have a valid OAuth access token, you are ready to construct the API headers that you will pass to the API call. Construct your headers like this, but make sure to substitute your valid OAuth access token for {ACCESS_TOKEN}.

declare -a Headers=('--header' "Authorization: Bearer ${ACCESS_TOKEN}"
'--header' "Accept: application/json"
'--header' "Content-Type: application/json")

Step 3. Construct the request body

Next, create the request body. Set your formIDs, roomID, and xFrameAllowedUrl domain in your request body. The formIDs allows you to add multiple forms to the form fill session. The xFrameAllowedUrl is the domain where you want to embed the form. Here’s how you make the request, but make sure to substitute your room ID for {room_id}, your form IDs for {library_form_id} and enter your domain next to xFrameAllowedUrl as shown in the gist.

request_data=$(mktemp /tmp/request-rooms.XXXXXX)
printf \
'{
"roomId": '$room_id',
"formIds": ["'$library_form_id'"],
"xFrameAllowedUrl": "https://iframetester.com"
}' >$request_data

Step 4. Call the Rooms API

Now that you have constructed your API headers and request body, make a request to the Rooms API to return an external form fill session. Here’s how you make the call, but make sure to substitute your API account ID for {account_id}.

response=$(mktemp /tmp/response-rooms.XXXXXX)
curl --request POST https://demo.rooms.docusign.com/restapi/v2/accounts/${account_id}/external_form_fill_sessions \
"${Headers[@]}" \
--data-binary @${request_data} \
--output ${response}
if grep -q FORM_NOT_IN_ROOM "$response"
then
echo ""
echo "" Problem: Selected room does not have any forms. Add a form to a room using example 4.
exit 0
else
echo ""
echo "URL to be Embedded:"
cat $response
echo ""
fi

Expected response

Copy the return url from the JSON response, and paste it in the corresponding line in the next step.

{
"url": "https://demo.rooms.docusign.com:443/Views/FormFillSession?token=eyJhbG...MDpcpY"
}

Step 5. Embed the form fill session

Paste the url generated in the prior step. Redirect your app’s internal browser to this URL to begin the external form fill session.

View the source code for this how-to on GitHub: Bash, C#, Java, Node, PHP, PowerShell, Python, or Ruby.

embed_url=`cat $response | grep url | sed 's/.*\"url\":\"//' | sed 's/\".*//'`
redirect_url="https://iframetester.com/?url=${embed_url}"
echo ""
echo "The embedded form URL is ${redirect_url}"
echo ""
echo "Attempting to automatically open your browser..."

Expected response

You will be redirected to the return URL to start the embedded external form fill session in your browser.

Note: Select SAVE & CLOSE to save the completed form to your room. In your Rooms account, right click on the specified Room, then select Documents to see this completed form.

Next steps