Get An Access Token in Node.js
This example shows how to authenticate with Legalesign's GraphQL API using Node.js and get your user details to verify everything works.
Installation
First, install the required dependencies:
npm install aws-sdk node-fetch
Command Line Example
auth-example.js
const AWS = require('aws-sdk');
const fetch = require('node-fetch');
class LegalesignAuth {
constructor() {
// General purpose pool credentials
this.userPoolId = 'eu-west-2_NUPAjABy7';
this.clientId = '38kn0eb9mf2409t6mci98eqdvt';
this.region = 'eu-west-2';
this.cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({
region: this.region
});
}
async getAccessToken(username, password) {
try {
const params = {
AuthFlow: 'USER_PASSWORD_AUTH',
ClientId: this.clientId,
AuthParameters: {
USERNAME: username,
PASSWORD: password
}
};
const result = await this.cognitoIdentityServiceProvider.initiateAuth(params).promise();
return result.AuthenticationResult.AccessToken;
} catch (error) {
throw new Error(`Authentication failed: ${error.message}`);
}
}
async makeGraphQLRequest(token, query) {
const response = await fetch('https://graphql.uk.legalesign.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ query })
});
return response.json();
}
}
// Example usage
async function main() {
const [username, password] = process.argv.slice(2);
if (!username || !password) {
console.error('Usage: node auth-example.js <username> <password>');
process.exit(1);
}
try {
const auth = new LegalesignAuth();
// Get access token
console.log('Getting access token...');
const token = await auth.getAccessToken(username, password);
// Test with a simple query
const query = `
query myInfo {
user {
id
firstName
lastName
email
}
}
`;
console.log('Making GraphQL request...');
const result = await auth.makeGraphQLRequest(token, query);
console.log('User info:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error:', error.message);
}
}
if (require.main === module) {
main();
}
module.exports = LegalesignAuth;
Run the Example
node auth-example.js your-username your-password
Expected output:
{
"data": {
"user": {
"id": "dXNyNjBkNWNkODktMDg3NS00ZXC67mItZjI3ODA5NjgwMDdl",
"firstName": "Your",
"lastName": "Name",
"email": "your.email@example.com"
}
}
}
Using in Your Application
const LegalesignAuth = require('./auth-example');
const auth = new LegalesignAuth();
// Get token once, reuse for multiple requests
const token = await auth.getAccessToken('username', 'password');
// Make any GraphQL query
const result = await auth.makeGraphQLRequest(token, `
query getDocuments {
documents {
id
name
status
}
}
`);
Next Steps
Now you can authenticate and make GraphQL requests. Find examples for your specific use case in the documentation.