Is it possible to query Loki through Grafana API (get the raw contents of a panel)?

Summary:
Is it possible to query Loki through Grafana API, or maybe get the raw contents of a panel?

Details:
I want to build an application that adapts logs for easier monitoring. The idea is to get a panel’s query, query Loki with it, then process the logs.

However, Loki is firewalled inside a cluster, so I have no access to it directly. Which is strange - I thought the frontend queried Loki, so I would need to have access, but seems like Grafana itself proxies the request.

So, would it be possible in any way to get the raw Loki response through Grafana’s API? Or maybe even “the raw response for a specific panel”.

I’ve figured it out. Here is an example query.

Endpoint: /api/ds/query
POST:

{
  "queries":[
    {
    "refId": "A",
      "datasource":{
        "type": "loki",
        "uid":"<get your Loki UID from /api/datasources>"
      },
      "queryType": "range",
      "expr": "{cluster=\"mycluster\", namespace=~\"mynamespace\", pod=~\"mypod\", container=\"mycontainer\"} |~ \"(?i)(error|warning)\""
      }
  ],
  "from":"now-1h",
  "to":"now"
}

Remember to escape the quotes!