Search code examples
pythonapifile-uploadgraphqlmutation

How to upload a file in Autentique API using Python?


Autentique API is Graphql. Documentation: https://docs.autentique.com.br/api/integracao/criando-um-documento


Solution

  • You must create an account on Autentique and an API key first.

    Uploading the file in sandbox and sending it to an email for signing. It returns document's id and name.

    Using curl

      curl -H "Authorization: Bearer <TOKEN>" https://api.autentique.com.br/v2/graphql \
      -F operations='{"query": "mutation CreateDocumentMutation($document: DocumentInput! $signers: [SignerInput!]! $file: Upload!) {createDocument(sandbox: true, document: $document, signers: $signers, file: $file) {id name }}", "variables": { "document": {"name": "<DOCUMENT_NAME>"}, "signers": [{"email": "<FROM_EMAIL>","action": "SIGN"}], "file": null } }' \
      -F map='{ "0": ["variables.file"] }' \
      -F 0=@<FULL_PATH_FILE>
    

    Using aiogqlc

    https://github.com/DoctorJohn/aiogqlc

    import asyncio
    from aiogqlc import GraphQLClient
    
    endpoint = "https://api.autentique.com.br/v2/graphql"
    
    headers = {
        "Authorization": "Bearer <TOKEN>"
    }
    
    client = GraphQLClient(endpoint, headers=headers)
    
    async def create_document():
        query = """
          mutation CreateDocumentMutation(
            $document: DocumentInput!
            $signers: [SignerInput!]!
            $file: Upload!
            ) {
            createDocument(
            sandbox: true,
            document: $document,
            signers: $signers,
            file: $file) 
            {
              id
              name
              }
            }"""
    
        variables = {
            "document": {
                "name": "<DOCUMENT_NAME>"
            },
            "signers": [{
                "email": "<FROM_EMAIL>",
                "action": "SIGN"
            }],
            "file": open('<FULL_PATH_FILE>', 'rb'),
        }
    
        response = await client.execute(query, variables=variables)
        print(await response.json())
    
    
    if __name__ == '__main__':
        asyncio.get_event_loop().run_until_complete(create_document())
    

    For more languages implementations: https://github.com/jaydenseric/graphql-multipart-request-spec#implementations