Symfony

Example for Symfony 5.4 and PHP 7.4

This Symfony snippet describes the sequence of API calls needed to create and activate a Signature Request.

Install Symfony http client

composer require symfony/http-client

Environment configuration

Add your API key in the .env file:

YOUSIGN_SANDBOX_API_KEY=your_key

HTTP client configuration

Edit config/package/http_client.yaml:

framework:
  http_client:
    scoped_clients:
      yousign.client:
        base_uri: 'https://api-sandbox.yousign.app/v3/'
        headers:
          Accept: 'application/json'
          Authorization: 'Bearer %env(YOUSIGN_SANDBOX_API_KEY)%'
use Symfony\Contracts\HttpClient\HttpClientInterface;

class YousignApiClient
{
    private HttpClientInterface $yousignClient;

    public function createSignatureRequest(): string
    {
        $response = $this->yousignClient->request(
            'POST',
            'signature_requests', 
            [
              'body' => <<<JSON
                      {
                        "name": "The name of your Signature Request",
                        "delivery_mode": "none",
                        "timezone": "Europe/Paris"
                      }
                      JSON,
              'headers' => [
                  'Content-Type' => 'application/json',
              ],
        	]
        );

        return $response->getContent();
    }
 }
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;

class YousignApiClient
{
    private HttpClientInterface $yousignClient;

    public function addDocumentToSignatureRequest(): string
    {
        $signatureRequestId = 'c3fd4243-de20-48b3-849a-ca1012ed2b59';
        $filePath = '/path/to/file/test.pdf';
        $fileName = 'test.pdf';

        $formFields = [
            'nature' => 'signable_document',
            'file' => DataPart::fromPath($filePath, $fileName, 'application/pdf'),
        ];

        $formData = new FormDataPart($formFields);
        $headers = $formData->getPreparedHeaders()->toArray();

        $response = $this->yousignClient->request(
            'POST', 
             sprintf('signature_requests/%s/documents', $signatureRequestId), 
             [
               'headers' => $headers,
               'body' => $formData->bodyToIterable(),
             ]
        );

        return $response->getContent();
    }
}
use Symfony\Contracts\HttpClient\HttpClientInterface;

class YousignApiClient
{
    private HttpClientInterface $yousignClient;

    public function addSignerToSignatureRequest(): string
    {
        $signatureRequestId = 'c3fd4243-de20-48b3-849a-ca1012ed2b59';
        $documentId = '34972edf-d033-40c0-9305-3341c3ce3744';

        $response = $this->yousignClient->request(
          	'POST', 
            sprintf('signature_requests/%s/signers', $signatureRequestId), 
          	[
            	'body' => <<<JSON
                       {
                           "info":{
                              "first_name":"John",
                              "last_name":"Doe",
                              "email":"[email protected]",
                              "locale":"fr"
                           },
                           "fields":[
                              {
                                 "type":"signature",
                                 "document_id": $documentId,
                                 "page":1,
                                 "x":77,
                                 "y":581
                              }
                           ],
                           "signature_level":"electronic_signature",
                           "signature_authentication_mode":"no_otp"
                       }
                       JSON,
            	'headers' => [
                'Content-Type' => 'application/json',
            	],
        ]);

        return $response->getContent();
    }
 }
use Symfony\Contracts\HttpClient\HttpClientInterface;

class YousignApiClient
{
    private HttpClientInterface $yousignClient;

    public function activateSignatureRequest(): string
    {
        $signatureRequestId = 'c3fd4243-de20-48b3-849a-ca1012ed2b59';
      
        $response = $this->yousignClient->request(
          	'POST', 
            sprintf('signature_requests/%s/activate', $signatureRequestId)
        );

        return $response->getContent();
    }
 }