Setting up snapshot api for beginners

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

Nice! Now read this part of the docu carefully specifically as it relates to external

JSON Body schema:

yes I applied the “external”:True such that it puts it on the cloud but that doesn’t work. How could I solve that?

1 Like

Read the doc again. When using external there are other required parameters

1 Like

I have added a key and delete key but still get the same result. I can define those keys myself right?

it looks like this;

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

In the docu if you notice it says in the Note section

…including the snapshot data.

A snapshot cannot reach back to your grafana and get the data from the data source(s) in your dashboard…which makes sense.

Please read this thread

I have made some progress and understood what I have to do to make it work, however I do not understand where I can extract the right json file of the dashboard for generating a snapshot, like the code provided at the request part of point three in this post: Snapshot using http api does nothing - #10 by mefraimsson

I found a way to write code for extracting the correct data from the dashboard for which I want to generate a dashboard, I now need to have the right json code to supply that data too. Can this json template be found on the grafana database, or does it need to be manually coded?

Any advice would be appreciated, thank you.

Have you tried grafana image renderer plugin?
You just have to post request on your grafana instance (http://grafana:3000/render/d?&someparameters)

This will automatically generate png file on your side.

1 Like