Search code examples
pythonapipython-requestspostman

Using both API Key & Basic Auth in Python or Postman


I have an API end-point that I have been given an API key for, but it also looks to have basic auth in front of it.

Can someone explain how I can use both basic Auth and an apiKey with Postman, or requests in python?

When I use the authentication section in Postman it will only let me pick one or the other.


Solution

  • Python:

    import requests
    
    url = 'apiendpoint'
    api_key = 'api-key'
    username = 'username'
    password = 'password'
    
    session = requests.Session()
    session.auth = (username, password)
    session.headers['X-API-Key'] = api_key
    response = session.get(url)
    

    Postman:

    In Postman, go to the "Authorization" tab in the request builder.

    Select "Basic Auth" from the "Type" dropdown and enter your username and password for Basic Authentication.

    Authentication Tab

    for Api-Key go to Headers tab next to Authorization tab and enter 'X-API-key' in key section and your api key in value.

    For generate basic authentication using base64 click on link Basic Authentication Header Generator