docusignapi

Automatic Consent for Sending Envelopes with Docusign API in Laravel Application


I’ve implemented Docusign in my Laravel application, and I’m currently using JWT Grant Authentication for sending documents to clients for signing. The integration is working fine, but I’ve encountered an issue: When I send envelopes with documents for clients to sign, initially, I have to manually provide consent via the Docusign consent page.

Since my application is designed to function as a backend microservice that sends documents automatically via triggers, this manual consent process interrupts the flow. I’d like to ask if there’s a way to automate this consent process, so the documents can be sent without the need for manual intervention.

Is there any configuration or API feature that allows automatic consent or pre-authorization for the app, or any best practices around this when using DocuSign in such a backend service?

This is my Service class handling preparing and sending the envelopes.

 public function getToken(ApiClient $apiClient): string
    {
        try {
            $privateKey = file_get_contents(storage_path(env('DS_KEY_PATH')), true);
            $response = $apiClient->requestJWTUserToken(
                env('DS_CLIENT_ID'),
                env('DS_IMPERSONATED_USER_ID'),
                $privateKey,
                env('DS_JWT_SCOPE')
            );
            $token = $response[0];
            return $token->getAccessToken();
        } catch (\Throwable $th) {
            if (strpos($th->getMessage(), 'consent_required') !== false) {
                throw new \Exception('consent_required');
            }
            throw $th;
        }
    }

    /**
     * Build and return the consent URL
     *
     * @return string
     */
    public function getConsentUrl(): string
    {
        return "https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature%20impersonation&client_id="
            . env('DS_CLIENT_ID')
            . "&redirect_uri=" . urlencode(env('DS_REDIRECT_URI'));
    }

    /**
     * Build the envelope definition from the request
     *
     * @param Request $request
     * @return EnvelopeDefinition
     */
    public function buildEnvelope(Request $request): EnvelopeDefinition
    {
        $fileContent = $request->file('formFile')->get();
        $fileName = $request->file('formFile')->getClientOriginalName();
        $fileExtension = $request->file('formFile')->getClientOriginalExtension();
        $recipientEmail = $request['email'];
        $recipientName = $request['name'];

        $document = new Document([
            'document_id' => "1",
            'document_base64' => base64_encode($fileContent),
            'file_extension' => $fileExtension,
            'name' => $fileName
        ]);

        $sign_here_tab = new SignHere([
            'anchor_string' => "FIRMA CLIENTE",
            'page_number' => "1",
            'anchor_units' => "pixels",
            'anchor_x_offset' => "40",
            'anchor_y_offset' => "40"
        ]);

        $tabs = new Tabs([
            'sign_here_tabs' => [$sign_here_tab]
        ]);

        $signer = new Signer([
            'email' => $recipientEmail,
            'name' => $recipientName,
            'recipient_id' => "1",
            'tabs' => $tabs
        ]);

        $recipients = new Recipients([
            'signers' => [$signer]
        ]);

        $inlineTemplate = new InlineTemplate([
            'recipients' => $recipients,
            'sequence' => "1"
        ]);

        $compositeTemplate = new CompositeTemplate([
            'composite_template_id' => "1",
            'document' => $document,
            'inline_templates' => [$inlineTemplate]
        ]);

        return new EnvelopeDefinition([
            'composite_templates' => [$compositeTemplate],
            'email_subject' => "Please sign",
            'status' => "sent"
        ]);
    }

    /**
     * Send the envelope using the DocuSign API
     *
     * @param ApiClient $apiClient
     * @param string $accountId
     * @param EnvelopeDefinition $envelopeDefinition
     * @return object
     */
    public function sendEnvelope(ApiClient $apiClient, string $accountId, EnvelopeDefinition $envelopeDefinition): object
    {
        $envelopeApi = new EnvelopesApi($apiClient);
        return $envelopeApi->createEnvelope($accountId, $envelopeDefinition);
    }

Solution

  • You can use organization level consent https://developers-d.docusign.com/platform/auth/consent/obtaining-admin-consent-external/ if all your users are on the same organization/account

    If that's not the case, you can work with the partner team to find a solution that is more suitable for ISVs so you don't have to run into the consent issue.