[βœ… Solution] Sending a Base64 File via API Using JS and GraphQL

Hello Gentlemen, I hope you are doing well. I am working on a project where I need to connect our CRM (Tape) to a digital signature platform. I would like some help to understand where I might be going wrong in constructing the code for sending client data and the contract via API. In my scenario, I have to use GraphQL in the code to be able to create the file on the signature platform. Can you help me by validating the code and pointing out possible corrections? Regards

import 'whatwg-fetch';

function downloadFileAndConvertToBase64(url: string): Promise<string> {
    return fetch(url)
        .then(response => response.blob())
        .then(blob => new Promise<string>((resolve, reject) => {
            const reader = new FileReader();
            reader.onloadend = () => resolve(reader.result as string);
            reader.onerror = reject;
            reader.readAsDataURL(blob);
        }));
}

const mutation = `
  mutation CreateDocumentMutation(
    $document: DocumentInput!,
    $signers: [SignerInput!]!,
    $file: Upload!,
    $organization_id: Int
  ) {
    createDocument(
      document: $document,
      signers: $signers,
      file: $file,
      organization_id: $organization_id
    ) {
      id
      name
      refusable
      sortable
      created_at
      signatures {
        public_id
        name
        email
        created_at
        action { name }
        link { short_link }
        user { id name email }
      }
    }
  }
`;

const url = contrato_field_files_file_url

downloadFileAndConvertToBase64(url).then(base64String => {
    const data = JSON.stringify({
        query: mutation,
        variables: {
            document: {
                name: contrato_field_files_filename,
            },
            signers: [
                { email: "xx@dat4force.com.br", action: "SIGN" },
                { email: "xx@outlook.com.br", action: "SIGN" }
            ],
            file: base64String
        }
    });

    fetch('https://api.autentique.com.br/v2/graphql', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        },
        body: data
    })
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('Erro na requisição:', error));
});

Hi @bruno.freitas,

based on your code I assume you were trying to consume the Autentique API to send a document from Tape.

We just added functionality to the workflow execution script environment and added a guide that shows you how to get this done in Tape:

Let me know if that solves your problem / question.

Cheers
Tim

PS: It might be worth noting that your script uses an invalid import statement on top. It is not possible to import anything in Tape workflow automation scripts, due to security reasons. Just as a heads up for the future.

1 Like