Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Vultr Go API client

License

NotificationsYou must be signed in to change notification settings

vultr/govultr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automatic ReleaserPkgGoDevUnit/Coverage TestsGo Report Card

The official Vultr Go client - GoVultr allows you to interact with the Vultr V2 API.

Installation

go get -u github.com/vultr/govultr/v3

Usage

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.

Example Client Setup

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)...

Example Usage

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)}

Pagination

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    }}

Versioning

This project followsSemVer for versioning. For theversions available, see thetags on thisrepository.

Documentation

See our documentation fordetailed information about API v2.

See ourGoDoc documentation for more details about this client's functionality.

Contributing

Feel free to send pull requests our way! Please see thecontributing guidelines.

License

This project is licensed under the MIT License - see theLICENSE.md file for details.


[8]ページ先頭

©2009-2025 Movatter.jp