Skip to main content

Client Setup

import (
    "context"
    "fmt"
    "os"

    openapiclient "github.com/baselinehq/pricingapi-client-golang"
)

token := os.Getenv("COSTGRAPH_API_KEY")

configuration := openapiclient.NewConfiguration()
configuration.DefaultHeader["Authorization"] = fmt.Sprintf("Bearer %s", token)

client := openapiclient.NewAPIClient(configuration)

Compute Pricing

instance := *openapiclient.NewGithubComBaselinehqGolangSharedTypesInstance()
instance.SetInstanceType("s-2vcpu-2gb")
instance.SetUsageType("ONDEMAND")
instance.SetProvider("DigitalOcean")
instance.SetOperatingSystem("linux")
instance.SetService("Droplet")
instance.SetRegion("nyc1")

cpuCores := float32(2)
ramGb := float32(2)
instance.SetVm(openapiclient.GithubComBaselinehqGolangSharedTypesVM{
    CpuCores: &cpuCores,
    RamGb:    &ramGb,
})

resp, _, err := client.DefaultAPI.
    PricingComputePost(context.Background()).
    Instance(instance).
    Execute()

if err != nil {
    fmt.Fprintf(os.Stderr, "error: %v\n", err)
    return
}

fmt.Printf("Cost per hour: %v\n", resp)

Recommendations

instance := *openapiclient.NewGithubComBaselinehqGolangSharedTypesInstance()
instance.SetInstanceType("s-4vcpu-16gb-amd")
instance.SetUsageType("ondemand")
instance.SetProvider("digitalocean")
instance.SetOperatingSystem("linux")
instance.SetService("Droplet")
instance.SetRegion("nyc1")

cpuCores := float32(4)
ramGb := float32(16)
instance.SetVm(openapiclient.GithubComBaselinehqGolangSharedTypesVM{
    CpuCores: &cpuCores,
    RamGb:    &ramGb,
})

resp, _, err := client.DefaultAPI.
    RecommendationsComputePost(context.Background()).
    Instance(instance).
    Execute()

if err != nil {
    fmt.Fprintf(os.Stderr, "error: %v\n", err)
    return
}

fmt.Printf("Recommendations: %v\n", resp)

Providers

resp, _, err := client.DefaultAPI.
    ProvidersGet(context.Background()).
    Execute()

if err != nil {
    fmt.Fprintf(os.Stderr, "error: %v\n", err)
    return
}

fmt.Printf("Providers: %v\n", resp)

Disk Pricing

disk := *openapiclient.NewTypesDisk()
disk.SetProvider("AWS")
disk.SetService("AmazonEBS")
disk.SetRegion("us-east-1")
disk.SetType("gp3")
disk.SetUsageType("ONDEMAND")
disk.SetCapacityGb(100)

resp, _, err := client.DefaultAPI.
    PricingDisksPost(context.Background()).
    Instance(disk).
    Execute()

if err != nil {
    fmt.Fprintf(os.Stderr, "error: %v\n", err)
    return
}

fmt.Printf("Disk pricing: %v\n", resp)

Disk Recommendations

disk := *openapiclient.NewTypesDisk()
disk.SetProvider("AWS")
disk.SetService("AmazonEBS")
disk.SetRegion("us-east-1")
disk.SetType("gp3")
disk.SetUsageType("ONDEMAND")
disk.SetCapacityGb(100)

diskReq := *openapiclient.NewTypesDiskRequest()
diskReq.SetInstance(disk)

resp, _, err := client.DefaultAPI.
    RecommendationsDisksPost(context.Background()).
    Instance(diskReq).
    Execute()

if err != nil {
    fmt.Fprintf(os.Stderr, "error: %v\n", err)
    return
}

fmt.Printf("Disk recommendations: %v\n", resp)