Skip to main content

Send a Document

In this step you'll use the send mutation to send a document for signing. This is the most common operation in the Legalesign API.

Prerequisites

You need:

  • A valid access token (from the previous steps)
  • A group ID — the team the document belongs to
  • A template ID — the PDF template to send

You can find both by querying your groups and their templates — see the previous step.

Send the document

index.js
const sendResult = await graphql(token, `
mutation SendDocument($input: DocumentSendSettingsInput!) {
send(input: $input)
}
`, {
input: {
groupId: '<your-group-id>',
templateId: '<your-template-id>',
title: 'Test Document',
recipients: [
{
id: '<role-id-from-template>',
firstName: 'Jane',
lastName: 'Smith',
email: 'jane@example.com',
role: 'Signer',
roleId: '<role-id-from-template>',
order: 1,
signerIndex: 1
}
]
}
});

console.log('Task ID:', sendResult.data.send);

The id and roleId come from the template's roles — query them with template { roles { id signerIndex } } (see the previous step).

The send mutation returns a task ID. The document is processed asynchronously.

Check the task status

Poll the task query to check when sending is complete:

index.js
const taskId = sendResult.data.send;

const status = await graphql(token, `
query CheckTask($id: ID!) {
task(id: $id) {
id
status
listings
}
}
`, { id: taskId });

console.log(JSON.stringify(status, null, 2));

When status is COMPLETE, the document has been sent and the recipient will receive an email invitation to sign.

Full working example

Here's the complete index.js combining all steps:

index.js
const LEGALESIGN_AUTH_ENDPOINT = 'https://cognito-idp.eu-west-2.amazonaws.com';
const CLIENT_ID = '<your-client-id>';
const USERNAME = '<your-username>';
const PASSWORD = '<your-password>';
const GRAPHQL_ENDPOINT = 'https://graphql.uk.legalesign.com/graphql';

async function getToken() {
const response = await fetch(LEGALESIGN_AUTH_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/x-amz-json-1.1',
'X-Amz-Target': 'AWSCognitoIdentityProviderService.InitiateAuth'
},
body: JSON.stringify({
AuthFlow: 'USER_PASSWORD_AUTH',
ClientId: CLIENT_ID,
AuthParameters: { USERNAME, PASSWORD }
})
});
const data = await response.json();
if (data.__type) throw new Error(`Auth failed: ${data.message}`);
return data.AuthenticationResult.AccessToken;
}

async function graphql(token, query, variables = {}) {
const response = await fetch(GRAPHQL_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ query, variables })
});
const result = await response.json();
if (result.errors) console.error('GraphQL errors:', result.errors);
return result;
}

async function main() {
const token = await getToken();
console.log('Authenticated.');

const result = await graphql(token, `
mutation SendDocument($input: DocumentSendSettingsInput!) {
send(input: $input)
}
`, {
input: {
groupId: '<your-group-id>',
templateId: '<your-template-id>',
title: 'Test Document',
recipients: [
{
id: '<role-id-from-template>',
firstName: 'Jane',
lastName: 'Smith',
email: 'jane@example.com',
role: 'Signer',
roleId: '<role-id-from-template>',
order: 1,
signerIndex: 1
}
]
}
});

console.log('Task ID:', result.data.send);
}

main().catch(console.error);

Next steps

Export This Article

Save a copy of this page as PDF or plain text.