Cannot access api.php Json from localhost or different server

Expected Behaviour:

user@chaos:~ $ curl http://chaos.lan/admin/api.php
{
  "domains_being_blocked": 166633,
  "dns_queries_today": 28775,
  "ads_blocked_today": 2614,
  "ads_percentage_today": 9.084274,
  "unique_domains": 929,
  "queries_forwarded": 14607,
  "queries_cached": 11530,
  "clients_ever_seen": 2,
  "unique_clients": 2,
  "dns_queries_all_types": 28775,
  "reply_UNKNOWN": 61,
  "reply_NODATA": 10823,
  "reply_NXDOMAIN": 6052,
  "reply_CNAME": 5463,
  "reply_IP": 6227,
  "reply_DOMAIN": 71,
  "reply_RRNAME": 0,
  "reply_SERVFAIL": 0,
  "reply_REFUSED": 0,
  "reply_NOTIMP": 0,
  "reply_OTHER": 0,
  "reply_DNSSEC": 0,
  "reply_NONE": 0,
  "reply_BLOB": 78,
  "dns_queries_all_replies": 28775,
  "privacy_level": 0,
  "status": "enabled",
  "gravity_last_updated": {
    "file_exists": true,
    "absolute": 1672002436,
    "relative": {
      "days": 0,
      "hours": 23,
      "minutes": 6
    }
  }
}

Actual Behaviour:

user@chaos:~ $ curl -v http://chaos.lan/admin/api.php
*   Trying 192.168.1.3:80...
* Connected to chaos.lan (192.168.1.3) port 80 (#0)
> GET /admin/api.php HTTP/1.1
> Host: chaos.lan
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
< Set-Cookie: PHPSESSID=qtk5ekobjuqsmgh9csqpmliips; path=/; HttpOnly; SameSite=Strict
< Content-type: application/json
< X-Pi-hole: The Pi-hole Web interface is working!
< X-Frame-Options: DENY
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Security-Policy: default-src 'self' 'unsafe-inline';
< X-Permitted-Cross-Domain-Policies: none
< Referrer-Policy: same-origin
< Content-Length: 2
< Date: Mon, 26 Dec 2022 20:51:04 GMT
< Server: lighttpd/1.4.59
<
* Connection #0 to host chaos.lan left intact
[]

Debug Token:

https://tricorder.pi-hole.net/KudGlJQC/

Additional Info:

I have the latest Pi-Hole release running on Raspi Lite 64bits with CORS_HOSTS defined:

user@chaos:~ $ uname -a
Linux chaos 5.15.76-v8+ #1597 SMP PREEMPT Fri Nov 4 12:16:41 GMT 2022 aarch64 GNU/Linux

user@chaos:~ $ cat /etc/lighttpd/conf-available/05-setenv.conf
# /usr/share/doc/lighttpd/setenv.txt.gz
server.modules                += ( "mod_setenv" )
setenv.add-environment = (
  "CORS_HOSTS" => "media.lan"
)

My goal is to get the Pi-Hole stats into Homer application, proxied through media.lan domain with Nginx. First, I just want to run a curl and get the data, but it returns nothing, either from PiHole host chaos.lan or Nginx host atlas.lan:

user@chaos:~ $ curl http://chaos.lan/admin/api.php
[]
user@atlas:~ $ curl http://chaos.lan/admin/api.php
[]

CORS works as expected:

user@atlas:~ $ curl http://chaos.lan/admin/api.php -H 'Origin: http://bogus.lan'
Failed CORS: bogus.lan vs 192.168.1.3, chaos.lan, pi.hole, localhost, media.lan

The interesting part is the stats show if I access them in a browser from zeus.lan, which is not part of CORS_HOSTS:

The API needs the authorization token.

You need to add &auth=<your_access_token> to the URL.

Note:
Since you are using the URL without any endpoints, you will need to add summaryRaw too.

/admin/api.php?summaryRaw&auth=<your_access_token>
1 Like

Thank you for the useful information, I presume the token is your WEBPASSWORD.

No.
It is a hash, based on your password.

Access Settings page, on the web interface and select API / Web interface tab
(or use the url /admin/settings.php?tab=api).

Click on Show API token button and copy the token.

1 Like

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