Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Generated API keys don't allow to use the testrail API


#1

Hi,
I’m trying to use an API Key instead of my TestRail UI password, but for some reason it doesn’t work, I can’t log in into the API using any generated api key "TestRail API returned HTTP 401(“Authentication failed: invalid or missing user/password or session cookie.”)"

can you please help me?

Thanks,


Difficulty to access TestRail via its API (error 401)
#2

Hi Diego,

Thanks for your posting. Please make sure to also specify the email address as part of the API call and the API keys are a drop in replacement for the user password. It’s also important to save the My Settings page first before you can use the API keys.

I hope this helps!

Regards,
Tobias


#3

Hello,
So I need to provide value for the user variable and then use the API key to make the call? I tried that but I still get a HTTP 401 error


#4

Hello Shiva,

Thanks for your posting! The API keys are a drop-in replacement for user passwords and you can authenticate API requests using the user email address and an API key. If you are using one of the API bindings, you can specify the API key via the password field/function:

http://docs.gurock.com/testrail-api2/start

I hope this helps!

Regards,
Tobias


#5

Hi,

I am facing similar issue. I am trying to use test rail api with api key and I am getting 401. I am using it with python. Here is the code I am trying(downloaded from http://docs.gurock.com/testrail-api2/bindings-python and just changed user and password part):

class APIClient:
    def __init__(self, base_url):
        self.user = 'MY_EMAIL'
        self.password = 'MY_API_KEY'  # api key

        if not base_url.endswith('/'):
            base_url += '/'
        self.__url = base_url + 'index.php?/api/v2/'

#6

Hi Rujoota,

Thanks for your posting! Sure, happy to help. The API keys are a drop-in replacement for the user password. Did you save the My Settings after adding the API key? Is there a reason why you modified the APIClient class instead of setting the username and password as shown in our example?

http://docs.gurock.com/testrail-api2/bindings-python

Cheers,
Tobias


#7

Hi ,

Even i am getting the 401 error . But i am not using the KEY . I am using the email id and the password that i use to login to the test rail local instance via browser . Am i missing something here .


#8

Hi @saradha,

Do you use one of our official API bindings? Please double-check that the username and password are correct and you can also try using an API key to see if this makes a difference.

Cheers,
Tobias


#9

Hi Tobias,

Yeah i downloaded the bindings from the site only . I am using the same password and id that i used to login to the testrail instance via browser. Also i am not able to use the key as i dont see that option in my settings page .


#10

Hi Saradha,

You can add API keys on your My Settings page on the API Keys tab:

Can you also try this please?

Cheers,
Tobias


#11

Hi Tobias,

I am not seeing this tab itself. Only the users and settings tab are visible in the My settings page.

Kindly help please

Thanks
Saradha


#12

Hi Saradha,

Which TR version do you use? The API keys were added with TR 4.2 and you can see the API Keys tab in the screenshot above (on the My Settings page).

Cheers,
Tobias


#13

Hi @tgurock

Am trying to access TestRail API using JavaScript. but i didn’t see any JavaScript binding mentioned in TestRail official bindings…! Can we access using JavaScript(nodeJS) ? I have tried placing API , usename to access TestRail API using plain nodejs get, post methods and also using frisby, but am still having the following error
"{ error: ‘Authentication failed: invalid or missing user/password or session cookie.’ }", with 401 Http response.

Thanks,
Harsha.


#14

Hi Harsha,

Thanks for your posting. Yes, you can also use TestRail from JavaScript/nodejs and there are already a few (customer-provided) example modules you can look into:

https://www.google.com/search?q=testrail+nodejs

When adding API keys, it’s important to save the My Settings form and only this would permanently store the API key in TestRail.

I hope this helps!

Cheers,
Tobias


#15

It just does not work
It works with password but not apikey name

yes, api key should be an actual character sequence, not the name of the api.
e.g.

curl -v -I -H “Content-Type: application/json” -u "jipijapo@company.com:dpobabwCV0g.S5w.Qyxx-.1fdn.dZn7sDZSadZ4gt" "“https://testrail.ezesoft.net/index.php?/api/v2/get_case/24042409


#16

Hi

When attempting this you are making sure that you are in fact saving the API key in the UI? if this is not saved, then this function will not work.


#17

It took me a few minutes and adding logging to testrail.py to understand tgurock’s answers but if anyone else is having trouble with using API keys in python, the simple/plain answer is thus:

import testrail
project_id = 123
client = testrail.APIClient(your_testrail_url)
client.user = 'username@yourorganization.com'   # not just username
client.password = 'xxx'  # the API key from testrail ---> My Settings ---> API Keys

response = client.send_get(f'get_cases/{project_id}')
print(f'Response: {response}')

#18

Thank you retsigam for the added clarification.