How to AUTH when accessing the PiHole API from python

All.

First of all, having discovered PiHole a few weeks ago I have to say what an awesome project. Now installed on a RaspberryPi-3, with some extra blocklists downloaded from Wally3K, it is happily doing its job protecting me and my family at home. You Guys Rock :slight_smile:

I have some Python successfully extracting stats from the admin/api.php with no authentication, but would like to also access some of the other stats requiring Authentication (e.g. topItems). However, I cannot work out how to specify this, not knowing the username or the Auth-Type.

Can anyone help ?

There's a few ways you can authenticate:

  1. Include your password in the pw variable when POSTing
  2. Pass the WEBPASSWORD hash found in /etc/pihole/setupVars.conf in the auth GET variable

If you keep a session, you will continue to be authenticated for up to 30min since your last API call. We are working to improve the security and usability of the API, so stay tuned for upcoming changes!

@Mcat12
YES !! This works. thankyou so much.
Here is my working example python code that demonstrates each of the api calls:

#! /usr/local/bin/python

import requests
import json

URL = "http://pi.hole/admin/api.php"
#
# Password-hash retrieved from /etc/pihole/setupVars.conf on the  pi.hole server
#
WEBPASSWORD = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
AUTH = "&auth=" + WEBPASSWORD

def get_pihole(url, query, auth=""):
        if auth == "":
                phpData = requests.get(url + query)
        else:
                phpData = requests.get(url + query + auth)

        jsonData = phpData.json()
        print (query + '=')
        print (str(jsonData) + '\n')
        return;

if __name__ == '__main__':
        #
        # No Authorisation required
        #
        get_pihole(URL, '?summary')
        get_pihole(URL, '?overTimeData10mins')

        #
        # Authorisation required
        #
        get_pihole(URL, '?topItems', AUTH )
        get_pihole(URL, '?topClients', AUTH )
        get_pihole(URL, '?getForwardDestinations', AUTH )
        get_pihole(URL, '?getQueryTypes', AUTH )
        get_pihole(URL, '?getAllQueries', AUTH )

Many thanks

Sean

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.