Search code examples
c#testrail

How to call TestRail with API Key?


Am trying to integrate NUnit with TestRail. Trying to create test run and execute required test cases from given Test Suite.

Am able to achieve the integration. But challenge is how to pass API Key?

So far, am able to make call to API by user name and password. But for security reason, I don't want to expose use name and password to others.

Here is the sample code:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

request.ContentType = "application/json";

request.Method = method;

string auth = Convert.ToBase64String( Encoding.ASCII.GetBytes("{mUser}:mPassword}" ) );

request.Headers.Add("Authorization", "Basic " + auth);

But am not sure, is there anyway to pass API Key?

Note: Am able to create API Key in TestRail


Solution

  • From the looks of TestRails documentation, it always seems to want a username and a secondary element. The secondary element being either your account password or an API key.

    Username and API Key
    Alternatively TestRail also supports API keys. API keys can be generated in TestRail under My Settings. With an API key you would still use HTTP basic authentication and you would still send your TestRail username (e.g. your email address). You would just replace your password with any of the generated/active API keys. You can generate multiple API keys for different systems or third-party tools and revoke access by deleting the API key at any time under My Settings. Source

    Obfuscating your username from the program would mean placing these details into your app.config file. If your publishing your code visibly on the other hand you may need to move it out of your code completely otherwise it would be visible to developers.

    At the very least though to answer the question, to pass your API key, replace your password with it in your current code.