D92 #100DaysOfCode

Today just felt like an API sort of day.

So sometimes I find myself creating Virtual Machines in Digital Ocean for just sandboxing or a quick PoC and I was thinking “Why don’t I just automate this” or make it a little easier.

So I decided to look into Digital Oceans API and see how easy it will be to hook into my account and play around.

It turns out Digital Ocean actually have a Pip package, you can find the install details here.

So I figured out how I can generate my account token so I can authenticate with my account, now I just needed to figure out how to put it all together in Python. Digital Ocean has some pretty good documentation so this was pretty easy. I also created an export so I didn’t need to hard code my API Token.

Now I had my token exported I needed to figure out how to pass that into my app, but that was done simply by importing os and setting a variable as my export.

    import digitalocean
    import os
    
    API_SECRET = os.environ.get("TOKEN")
    manager = digitalocean.Manager(token=API_SECRET)
    my_droplets = manager.get_all_droplets()
    print(my_droplets)

Now when I run this I get a list of my two running servers in my account.

code-capture

We are in action!

So what I plan on doing is making an app where I can create a small Linux box in the cloud whenever I need it, as well as delete it.

Written on December 21, 2019