Skip to main content

Get My Details

In this section we're going to request the simplest details possible from the Legalesign API in order to check that everything is working for you. This example will dump the results out into the terminal for you to see.

Command Line Example

Create a file called CLIExample.cs in your project and copy the code below into that file (don't worry we'll go into detail about what is happening later, as well as improve the code so that you can use it more generically).

CLIExample.cs
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Amazon.CognitoIdentity;
using Amazon.CognitoIdentityProvider;
using Amazon.Extensions.CognitoAuthentication;
using Amazon.Runtime;
using System.Threading.Tasks;

namespace CLILegalesignExamples
{
class CLILegalQLExample
{
static async Task Main(string[] args)
{
Console.WriteLine("Legalesign C# Command Line Example");
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://graphql.uk.legalesign.com/graphql")
};

// First we need a valid security token
string okes = await CLILegalQLExample.GetCredsAsync(args[0], args[1]);

httpClient.DefaultRequestHeaders.Add("User-Agent", "LegalesignCLI");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", okes);

var queryObject = new
{
query = @"query {
user {
email
firstName
lastName
}
}",
variables = new { }
};

var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
Content = new StringContent(JsonSerializer.Serialize(queryObject), Encoding.UTF8, "application/json")
};

dynamic responseObj;

using (var response = await httpClient.SendAsync(request))
{
response.EnsureSuccessStatusCode();

var responseString = await response.Content.ReadAsStringAsync();
if (responseString != null)
{
responseObj = JsonSerializer.Deserialize<dynamic>(responseString);
Console.WriteLine(responseObj);
}
}

Console.ReadLine();
}



static async Task<string> GetCredsAsync(string username, string password)
{
// These are the general purpose pool and client id - if you have dedicated ones insert them here.
var poolData = new
{
UserPoolId = "eu-west-2_NUPAjABy7",
ClientId = "38kn0eb9mf2409t6mci98eqdvt",
};

AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());
CognitoUserPool userPool = new CognitoUserPool(poolData.UserPoolId, poolData.ClientId, provider);
CognitoUser user = new CognitoUser(username, poolData.ClientId, userPool, provider);
InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest()
{
Password = password
};

AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);
return authResponse.AuthenticationResult.AccessToken;

}
}
}

Now alter the StartupObject in your .csproj file to look like this.

  <PropertyGroup>
...
<StartupObject>CLILegalesignExamples.CLILegalQLExample</StartupObject>
</PropertyGroup>

If you're using VS Code to create your project you may want to change the args that are sent when you run or debug this project, rather than having to execute the project from the command line.

To do this you can set them in your launch.json for the project. It should look something like this:

{
"version": "0.3.0",
"configurations": [
{
"name": "My debug config",
"type": "coreclr",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/net9.0/<projectname>.dll",
"args": ["apiuser@legalesign.com", "@p4sswordseu2london"]
}
]
}
info

If you don't wish to be bothered with arguments in this example, you can replace the arg[0] and arg[1] variables in the code with your username and password as strings.

Run the example

In VS Code you can debug or execute this code by pressing Ctrl-F5 or F5, or navigating to the debug panel and choosing the play icon for your launch setup. You should see something like the following in the Debug Console.

Sample execution in the debug console

If you receive authentication errors then check your account credentials. If you see the JSON with data like this:

{"data":{"user":{"email":"apiuser@legalesign.com","firstName":"Alex","lastName":"Why"}}}

Then your query has completed successfully and you've retrieved the details of your user account! Nice work. You might notice that we didn't specify the userId we were asking about. This is because (unlike all other types in Legalesign) the User object will assume you are talking about yourself without an ID. If you're keen to try out other queries before we move on to the more advanced examples, check out the GraphIQL Explorer.