Question

How do I get the ID from a snapshot during creation via API?

When I create a snapshot via API it returns an ID which I then store in my database. But when I list my snapshots later they all have different ID’s, is the snapshot ID changing over time or does the API return the wrong ID for some reason? are there any workarounds to this problem?

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hi @chadlwilliams, As @hansen already said, when you execute Snapshot actions, it returns an action ID, not a snapshot ID. API docs states it:

Name: id, Type: number, Description: A unique identifier for each Droplet action event. This is used to reference a specific action that was requested.

It’s not wrong ID, it’s just an action ID which you can use to ‘trace’ snapshot execution. Until creation is not complete, you will not have snapshot ID.

Action ID alongside Action endpoints is used to check action (in your case snapshot) details. You can learn more about it in API docs: Retrieve a Droplet Action or Retrieve an existing Action. There is result difference between above two, but Retrieve a Droplet Action requires you to provide Droplet ID while Retrieve an existing Action doesn’t.

When you query any of above Action endpoints, you get status of Action as well as other important details. If creation is in progress, status will be in-progress. Once it completes (successfully), it will be completed

Workaround is to query Action endpoint, with ID you got when executed snapshot request, until progress is not in-progress. Once it is completed, execute Get Snapshot to obtain its ID. Reason why we check is it not in-progress is we don’t know what status will be in case of fail, so you don’t risk stuck-ing into infinity loop. For example, something similar happens in doctl. It is querying Action endpoint every 5 seconds until Actions status is not in-progress. If you are more interested in it, you can see how does actionWait code looks like on GitHub.

Only thing you could pay attention here is rate limit. Rate limit is how much API requests you can execute per time unit. Once remaining rate limit reaches zero, you will not be able to execute API requests until limit restarts. API docs states that it’s currently limited to 5,000 requests per hour per token.

In most of cases you are safe, but for example if you are checking status for 10 snapshots in same time every 5 seconds, you can run out of it. If you get in problem like this, just raise time and you will be good to go.

If you get into any problem, feel free to ask anything. Maybe I went to much into details, but hopefully it will be helpful to you.

This is a SUPER stupid concept. It should definitely return the snapshot id at creation time, with a status like pending, processing, success…

With this stupid concept, how can I map the snapshots from API to my application object model? The answer is:

  1. I make an API call to create a droplet snapshot and I retrieve the stupid action id.
  2. I need recursively check the action status until it’s completed.
  3. The action is now completed, now I’ll have to get all snapshots of my droplet and find the latest one and map the reference id to my database.

By following this flow, from my end, I’ll have to make sure that my app won’t accept any other snapshot creation request if the latest action status is “in-progress”.

And here is the flow if DO returns snapshot id at creation time:

  1. I make an API call to create a snapshot -> I got the snapshot ID -> I save the ID to my DB with the status is “in-progress”
  2. Now I just need a schedule task for checking status of all snapshots where status = “in-progress”

And the best way should be using webhooks.

  1. I make api call -> I get snapshot id -> I save to my db
  2. DO successfully created the snapshot -> send webhook request to my app

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel