Python Authentication
This tutorial covers authenticating with the Legalesign GraphQL API in Python using the standard requests library — no vendor-specific SDK required.
tip
Using Cursor, Claude, or another AI coding tool? Connect it to the Legalesign docs for context-aware help as you follow this tutorial.
Installation
pip install requests
Example
auth_example.py
import requests
import json
import os
LEGALESIGN_AUTH_ENDPOINT = 'https://cognito-idp.eu-west-2.amazonaws.com'
CLIENT_ID = '<your-client-id>'
USERNAME = '<your-username>'
PASSWORD = '<your-password>'
GRAPHQL_ENDPOINT = 'https://graphql.uk.legalesign.com/graphql'
def get_access_token():
"""Get access token from Cognito InitiateAuth endpoint"""
response = requests.post(LEGALESIGN_AUTH_ENDPOINT, json={
'AuthFlow': 'USER_PASSWORD_AUTH',
'ClientId': CLIENT_ID,
'AuthParameters': {
'USERNAME': USERNAME,
'PASSWORD': PASSWORD
}
}, headers={
'Content-Type': 'application/x-amz-json-1.1',
'X-Amz-Target': 'AWSCognitoIdentityProviderService.InitiateAuth'
})
response.raise_for_status()
return response.json()['AuthenticationResult']['AccessToken']
def graphql(token, query, variables=None):
"""Make a GraphQL request with the access token"""
response = requests.post(
GRAPHQL_ENDPOINT,
headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
},
json={'query': query, 'variables': variables or {}}
)
response.raise_for_status()
return response.json()
def main():
token = get_access_token()
print('Authenticated.')
result = graphql(token, """
query {
user {
id
firstName
lastName
email
}
}
""")
print(json.dumps(result, indent=2))
if __name__ == "__main__":
main()
Run the Example
python auth_example.py
Environment Variables (Recommended)
For production, use environment variables instead of hardcoded values:
import os
CLIENT_ID = os.getenv('LEGALESIGN_CLIENT_ID')
USERNAME = os.getenv('LEGALESIGN_USERNAME')
PASSWORD = os.getenv('LEGALESIGN_PASSWORD')
Next Steps
Now you can authenticate and make GraphQL requests. See: