Skip to content

civo/civogo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8af7f28 · Mar 18, 2025
Mar 18, 2024
Sep 13, 2022
Jul 31, 2020
Jul 31, 2020
Nov 2, 2021
Jan 24, 2020
Jan 16, 2020
Mar 2, 2020
Dec 31, 2021
Apr 29, 2022
Oct 11, 2022
Oct 11, 2022
May 30, 2022
May 2, 2022
Feb 17, 2025
Aug 30, 2021
Aug 18, 2021
Feb 29, 2020
Dec 6, 2022
Sep 13, 2022
Jul 23, 2024
Mar 18, 2024
Mar 23, 2023
Jan 8, 2025
Jan 8, 2025
Nov 4, 2024
Sep 13, 2020
Oct 21, 2024
Jan 21, 2025
Sep 4, 2024
Dec 19, 2024
Aug 18, 2022
Jan 21, 2025
Jan 21, 2025
May 10, 2021
Mar 18, 2025
Oct 1, 2023
Oct 1, 2023
Jan 10, 2025
Jul 15, 2022
Jul 15, 2022
Jul 6, 2023
Apr 13, 2023
Jul 17, 2024
Mar 29, 2022
Nov 6, 2024
Nov 27, 2024
Nov 12, 2024
Nov 12, 2024
May 23, 2024
May 23, 2024
Jan 4, 2024
Sep 10, 2024
Sep 10, 2024
Aug 25, 2022
Sep 21, 2021
Sep 21, 2021
Jan 25, 2022
Aug 18, 2021
Mar 12, 2024
Sep 4, 2024
Sep 4, 2024
Jan 11, 2023
Jan 11, 2023
Nov 6, 2024
Jul 3, 2024
Aug 18, 2021
Aug 18, 2021
Nov 2, 2021
Nov 2, 2021
Dec 1, 2022
Dec 1, 2022
Aug 18, 2022
Jan 20, 2022
Nov 2, 2021
Nov 2, 2021
Oct 18, 2021
Oct 18, 2021
Jan 21, 2025
Jan 24, 2025
Jan 21, 2025
Jan 21, 2025
Oct 2, 2024
Oct 1, 2024
Aug 18, 2021
Apr 2, 2021

Repository files navigation

Civogo - The Golang client library for Civo

go.dev reference Build Status Lint

Civogo is a Go client library for accessing the Civo cloud API.

You can view the client API docs at https://pkg.go.dev/github.com/civo/civogo and view the API documentation at https://api.civo.com

Install

go get github.com/civo/civogo

Usage

import "github.com/civo/civogo"

From there you create a Civo client specifying your API key and a region. Then you can use public methods to interact with Civo's API.

Authentication

You will need both an API key and a region code to create a new client.

Your API key is listed within the Civo control panel's security page. You can also reset the token there, for example, if accidentally put it in source code and found it had been leaked.

For the region code, use any region you know exists, e.g. LON1. See the API documentation for details.

package main

import (
	"context"
	"github.com/civo/civogo"
)

const (
    apiKey = "mykeygoeshere"
    regionCode = "LON1"
)

func main() {
  client, err := civogo.NewClient(apiKey, regionCode)
  // ...
}

Examples

To create a new Instance:

config, err := client.NewInstanceConfig()
if err != nil {
  t.Errorf("Failed to create a new config: %s", err)
  return err
}

config.Hostname = "foo.example.com"

instance, err := client.CreateInstance(config)
if err != nil {
  t.Errorf("Failed to create instance: %s", err)
  return err
}

To get all Instances:

instances, err := client.ListAllInstances()
if err != nil {
  t.Errorf("Failed to create instance: %s", err)
  return err
}

for _, i := range instances {
    fmt.Println(i.Hostname)
}

Pagination

If a list of objects is paginated by the API, you must request pages individually. For example, to fetch all instances without using the ListAllInstances method:

func MyListAllInstances(client *civogo.Client) ([]civogo.Instance, error) {
    list := []civogo.Instance{}

    pageOfItems, err := client.ListInstances(1, 50)
    if err != nil {
        return []civogo.Instance{}, err
    }

    if pageOfItems.Pages == 1 {
        return pageOfItems.Items, nil
    }

    for page := 2;  page<=pageOfItems.Pages; page++ {
        pageOfItems, err := client.ListInstances(1, 50)
        if err != nil {
            return []civogo.Instance{}, err
        }

        list = append(list, pageOfItems.Items)
    }

    return list, nil
}

Error handler

​ In the latest version of the library we have added a new way to handle errors. Below are some examples of how to use the new error handler, and the complete list of errors is here. ​ This is an example of how to make use of the new errors, suppose we want to create a new Kubernetes cluster, and do it this way but choose a name that already exists within the clusters that we have: ​

// kubernetes config
configK8s := &civogo.KubernetesClusterConfig{
    NumTargetNodes: 5,
    Name: "existent-name",
}
// Send to create the cluster
resp, err := client.NewKubernetesClusters(configK8s)
if err != nil {
     if errors.Is(err, civogo.DatabaseKubernetesClusterDuplicateError) {
     // add some actions
     }
}

The following lines are new: ​

if err != nil {
     if errors.Is(err, civogo.DatabaseKubernetesClusterDuplicateError) {
     // add some actions
     }
}

In this way. we can make decisions faster based on known errors, and we know what to expect. There is also the option of being able to say this to account for some errors but not others: ​

if err != nil {
     if errors.Is(err, civogo.DatabaseKubernetesClusterDuplicateError) {
     // add some actions
     }
     if errors.Is(err, civogo.UnknownError) {
         // exit with error
     }
}

We can use UnknownError for errors that are not defined.

Contributing

If you want to get involved, we'd love to receive a pull request - or an offer to help over our KUBE100 Slack channel. Please see the contribution guidelines.