Friday, February 16, 2018

Digital Ocean API

Follow the steps to test Digital Ocean API.

# First create a personal access token from the website under API->Tokens/Keys 

1. To list all droplets. Change the key in red with yours:
#Using curl:
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer Your-Key" "https://api.digitalocean.com/v2/droplets?page=1&per_page=1" 
#Using python3:
import requests
headers = {'Content-Type': 'application/json', 'Authorization' : 'Bearer Your-Key'}
r = requests.get('https://api.digitalocean.com/v2/droplets?page=1&per_page=1', headers=headers)
r.json()

2. To list all firewalls. Change the key in red with yours:
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer Your-Key" "https://api.digitalocean.com/v2/firewalls"

3. To add SSH access in your firewall. Change the key in red with yours:
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer Your-Key" -d '{"inbound_rules":[{"protocol":"tcp","ports":"22","sources":{"addresses": ["0.0.0.0/0"]}}]}' "https://api.digitalocean.com/v2/firewalls/Firewall-ID/rules"

4. To remove SSH access from your firewall. Change the key in red with yours:
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer Your-Key" -d '{"inbound_rules":[{"protocol":"tcp","ports":"22","sources":{"addresses": ["0.0.0.0/0"]}}]}' "https://api.digitalocean.com/v2/firewalls/Firewall-ID/rules"

Get a 10$ credit from Digital Ocean by clicking this link: https://m.do.co/c/f99ba1bc2360

No comments:

Post a Comment