- Notifications
You must be signed in to change notification settings - Fork56
vultr/govultr
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The official Vultr Go client - GoVultr allows you to interact with the Vultr V2 API.
go get -u github.com/vultr/govultr/v3
Vultr uses a personal access token (PAT) to interact/authenticate with theAPIs. Generate an API Key from theAPI menuin the Vultr Customer Portal.
To instantiate a GoVultr client, invokeNewClient()
. Most operations requirethat you pass a PAT to anoauth2
library to create the*http.Client
, whichconfigures theAuthorization
header with your PAT as thebearer api-key
. Ifa PAT is not provided, public operations like listing plans or applicationswill still work.
The client has three optional parameters:
- BaseUrl: Change the Vultr default base URL
- UserAgent: Change the Vultr default UserAgent
- RateLimit: Set a delay between calls. Vultr limits the rate of back-to-back calls. Use this parameter to avoid rate-limit errors.
package mainimport ("context""os""github.com/vultr/govultr/v3""golang.org/x/oauth2")funcmain() {apiKey:=os.Getenv("VultrAPIKey")config:=&oauth2.Config{}ctx:=context.Background()ts:=config.TokenSource(ctx,&oauth2.Token{AccessToken:apiKey})vultrClient:=govultr.NewClient(oauth2.NewClient(ctx,ts))// Optional changes_=vultrClient.SetBaseURL("https://api.vultr.com")vultrClient.SetUserAgent("mycool-app")vultrClient.SetRateLimit(500)}
Passingnil
toNewClient
will work for routes that do not requireauthentication.
...vultrClient:=govultr.NewClient(nil)ctx:=context.Background()plans,_,_,err:=vultrClient.Plan.List(ctx,"",nil)...
Create a VPS
instanceOptions:=&govultr.InstanceCreateReq{Label:"awesome-go-app",Hostname:"awesome-go.com",Backups:"enabled",EnableIPv6:BoolToBoolPtr(false),OsID:362,Plan:"vc2-1c-2gb",Region:"ewr",}res,err:=vultrClient.Instance.Create(context.Background(),instanceOptions)iferr!=nil {fmt.Println(err)}
GoVultr v2 introduces pagination for all list calls. Each list call returns ameta
struct containing the total amount of items in the list andnext/previous links to navigate the paging.
// Meta represents the available pagination informationtypeMetastruct {Totalint`json:"total"`Links*Links}// Links represent the next/previous cursor in your pagination callstypeLinksstruct {Nextstring`json:"next"`Prevstring`json:"prev"`}
Pass aper_page
value to thelist_options
struct to adjust the number ofitems returned per call. The default is 100 items per page and max is 500 itemsper page.
This example demonstrates how to retrieve all of your instances, with oneinstance per page.
listOptions:=&govultr.ListOptions{PerPage:1}for {i,meta,err:=client.Instance.List(ctx,listOptions)iferr!=nil {returnnil,err }for_,v:=rangei {fmt.Println(v) }ifmeta.Links.Next=="" {break }else {listOptions.Cursor=meta.Links.Nextcontinue }}
This project followsSemVer for versioning. For theversions available, see thetags on thisrepository.
See our documentation fordetailed information about API v2.
See ourGoDoc documentation for more details about this client's functionality.
Feel free to send pull requests our way! Please see thecontributing guidelines.
This project is licensed under the MIT License - see theLICENSE.md file for details.
About
Vultr Go API client