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

Posted March 18, 2017 5.1k views

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?


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.

Submit an Answer
2 answers

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.

  • I guess I missed that detail then, so my workaround to this would therefore be to poll the “action” API call with the ID returned from snapshot creation until it’s status says “completed” and then use “resource_id” (I suppose) to obtain the real ID for the new snapshot. Thank you very much :)

    • Just a single correction - Not a resource_id, it represents object ID on which action was executed. In Snapshot case, it will always be Droplet ID which you snapshot, even when you complete it.

      You will have to list all Snapshots once action is completed and pull the snapshot ID there.
      If you want to be sure you pull right one, try comparing date/time of Action to Snapshot Creation one. I can’t test it now, but it should work.

      Also, as I said and note it one more time, when working with actions, it’s much safer to check inequality to “in-progress” rather then equality to “complete”.
      It’s not documented what it can return for status. I never had snapshot failure problem and I done many of them. But better be safe than stuck in infinity loops.

      If you have any more questions, feel free to ask. :)

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