Get An Access Token in Python
This example shows how to authenticate with Legalesign's GraphQL API using Python and get your user details to verify everything works.
Installation
Install the required dependencies:
pip install boto3 requests
Command Line Example
auth_example.py
import boto3
import requests
import json
import sys
from botocore.exceptions import ClientError
class LegalesignAuth:
def __init__(self):
# General purpose pool credentials
self.user_pool_id = 'eu-west-2_NUPAjABy7'
self.client_id = '38kn0eb9mf2409t6mci98eqdvt'
self.region = 'eu-west-2'
self.cognito_client = boto3.client('cognito-idp', region_name=self.region)
def get_access_token(self, username, password):
"""Get access token from AWS Cognito"""
try:
response = self.cognito_client.initiate_auth(
ClientId=self.client_id,
AuthFlow='USER_PASSWORD_AUTH',
AuthParameters={
'USERNAME': username,
'PASSWORD': password
}
)
return response['AuthenticationResult']['AccessToken']
except ClientError as e:
raise Exception(f"Authentication failed: {e.response['Error']['Message']}")
def make_graphql_request(self, token, query):
"""Make GraphQL request with access token"""
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
response = requests.post(
'https://graphql.uk.legalesign.com/graphql',
headers=headers,
json={'query': query}
)
response.raise_for_status()
return response.json()
def main():
if len(sys.argv) != 3:
print("Usage: python auth_example.py <username> <password>")
sys.exit(1)
username, password = sys.argv[1], sys.argv[2]
try:
auth = LegalesignAuth()
# Get access token
print("Getting access token...")
token = auth.get_access_token(username, password)
# Test with a simple query
query = """
query myInfo {
user {
id
firstName
lastName
email
}
}
"""
print("Making GraphQL request...")
result = auth.make_graphql_request(token, query)
print("User info:")
print(json.dumps(result, indent=2))
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
Run the Example
python auth_example.py your-username your-password
Expected output:
{
"data": {
"user": {
"id": "dXNyNjBkNWNkODktMDg3NS00ZXC67mItZjI3ODA5NjgwMDdl",
"firstName": "Your",
"lastName": "Name",
"email": "your.email@example.com"
}
}
}
Using in Your Application
from auth_example import LegalesignAuth
auth = LegalesignAuth()
# Get token once, reuse for multiple requests
token = auth.get_access_token('username', 'password')
# Make any GraphQL query
result = auth.make_graphql_request(token, """
query getDocuments {
documents {
id
name
status
}
}
""")
print(result)
Environment Variables (Recommended)
For production, use environment variables:
import os
username = os.getenv('LEGALESIGN_USERNAME')
password = os.getenv('LEGALESIGN_PASSWORD')
token = auth.get_access_token(username, password)
Next Steps
Now you can authenticate and make GraphQL requests. Find examples for your specific use case in the documentation.