Setting up snapshot api for beginners

  • What Grafana version and what operating system are you using?

Grafana * v9.0.3 (023f9251a9) on Mac OS

I am using grafana with influx db. Grafana is running on a remote server and I am accessing it trough an vpn.

  • What are you trying to achieve?
    I am quite new to developing and im trying to setup a grafana snapshot api to send snapshots of my dashboard to customers. I am stuck on the very beginning and do not now very wel how to get started.

So far I have been able to write some python code to fetch the json code of my dashboard, but I have not yet found how to get a snapshot trough the api.

I am getting my information form here: HTTP Snapshot API | Grafana documentation

What I do not understand is the code format on that web page. Where do I insert or use that code to make the api work? In python?

I also found this: https://apify.com/defensivedepth/grafana-generate-snapshots/source-code

But I do not know how to implement this myself.

Can anyone assist my in setting this up and getting started?

Thanks in advance

The code shown is the HTTP request that you have to send to the API.

The first few lines are the HTTP request and the header:

The request is:
POST /api/snapshots HTTP/1.1

The headers are:
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

Then there is a blank line, followed by the JSON body of the request.

Antony.

1 Like

And how will the customer use this snapshot? Is it a snapshot as in data snapshot or screen capture or pdf?

1 Like

How do a have to edit the HTTP request for my own dashboard? And how or with with program can I send the request ? Thank you

The customer will just get to look at a screen capture of the dashboard.

I do not believe the api does screen capture.

2 Likes

The API definitely does not do a screen capture.

You would need some sort of browser-renderer to achieve that.

The API ‘snapshot’ is a copy of the JSON which tells Grafana what to do to
create the dashboard; it is not the result of Grafana processing that JSON.

Antony.

1 Like

So is there no way to automate the snapshot procedure? What I basically want to do is to show the dashboard (which is the car battery charging state) to the customer when they submit their use information on our website, by generating a snapshot link. The dashboard should also be updating every couple of minutes so I would generate a new snapshot every new moment. Is there a better way to do this? Thank you for the advice

Pretty sure there are ways to automate this but it is outside of the scope of grafana. The other way is for you to give your customers view only access to their dashboards.

Or you can look at reporting if your grafana is enterprise.

1 Like

So is there no way to automate the snapshot procedure?

You can most definitely automate the snapshot procedure, but what you will get
is not a screenshot, which is the word I would use for what you seem to be
trying to create.

What I basically want to do is to show the dashboard

In that case you need a browser, or some software which knows how to render
like a browser, even if it doesn’t produce an image on a screen (maybe it
generates a PNG or a GIF instead).

https://wkhtmltopdf.org/ may be able to do what you want (I haven’t tried it
with Grafana).

(which is the car battery charging state) to the customer when they submit
their use information on our website, by generating a snapshot link.

I suggest you stop thinking of this image you want as a “snapshot”, because
that term means something entirely different in Grafana terms (and espcially if
you look at the API).

The dashboard should also be updating every couple of minutes so I would
generate a new snapshot every new moment.

Try doing some web searches for “headless browser”. I think it’ll get you
further than trying to do this with Grafana itself. You still need Grafana to
create the dashboard HTML, but some other tool is going to turn that into an
image for you to show to the customer.

Antony.

3 Likes

Oke thank you for the advice, I will look into those things. The reason I thought I could generate snapshots with an api as a screenshot is because when you share a dashboard on grafana and go to the snapshot option you get a link to the dashboard which you can then share with people outside the organisation, who would be the customer.

so when you create a snapshot you can create one locally or remotely.

I just pushed one here

https://snapshots.raintank.io/dashboard/snapshot/9MCcdZS57sgIBBkYhTcKWJKB1np2Snvc?orgId=2

It is not a screenshot per se, it is like a read only view of the grafana dashboard. Is that what you want to share? as @pooh said you are using grafana exclusive terms and conflating them with your own terminology which is creating some confusion.

So yes you can share snapshots outside of grafana.

1 Like

Yes it is that what I want to share, but then generate it automatically in some way

1 Like

In that case read the documentation for it here, try it out using python and if/when you are stuck after having tried a few things let us know.

2 Likes

So this (HTTP Snapshot API | Grafana documentation ) is what I have been reading on and trying to understand for a while. What I do not understand is how do I work with this code? Can I just use it in python? And how do I adapt it for my own dashboard?
Thank you

1 Like

Yes you can use it in python.

The first part is the verb and the api endpoint.

POST /api/snapshots

the second part if what you need to send in that POST

    Accept: application/json
    Content-Type: application/json
    Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

So let’s start with the above basic 2 key items. Do you know how to do this in python, have you done api calls from within python in the past?

1 Like

No this is the first time. I have already coded in python but not api’s.

search for python and api in this forum and you will find a few examples

Oké, thanks a lot.

Raf

1 Like

I have been able to write some python code that works to generate a snapshot trough the api. The only problem I have now is that when I set “external” to true such that it can be easily accessed outside the company, I get error messages. When I generate the dashboard locally it works. Can I solve this? it seems that there must be a solution for this since there is an option to generate the snapshot externally, as documented by grafana ( HTTP Snapshot API | Grafana documentation )

the error I get:

my code:

import requests
import json

url_dest = “https://MY_LINK”

desired_charger = “CP1”

headers_dest = {
“Accept”: “application/json”,
“Content-Type”: “application/json”,
“Authorization”: “Bearer MY_TOKEN”
}

s = requests.Session()
s.headers = headers_dest

snapshot_data = {
“external”: True,
“dashboard”: {…},
“expires”: 3600,
“name”: “snapshot api testing”
}

r = s.post(url_dest + “snapshots”, headers=headers_dest, verify=False, data=json.dumps(snapshot_data))

print(r.json())

Thanks in advance,
Raf