Convert Figma logo to code with AI

cloudflare logocloudflare-go

The official Go library for the Cloudflare API

1,427
567
1,427
36

Top Related Projects

1,421

DigitalOcean Go API client

AWS SDK for the Go programming language.

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:

Quick Overview

Cloudflare-go is the official Go library for interacting with Cloudflare's API v4. It provides a comprehensive set of bindings for Cloudflare's services, allowing developers to manage and automate various aspects of their Cloudflare accounts programmatically.

Pros

  • Officially maintained by Cloudflare, ensuring reliability and up-to-date API support
  • Comprehensive coverage of Cloudflare's API endpoints
  • Well-documented with clear examples and usage instructions
  • Actively maintained with regular updates and improvements

Cons

  • Some advanced features may require deeper understanding of Cloudflare's infrastructure
  • API changes in Cloudflare may occasionally lead to breaking changes in the library
  • Limited support for older Go versions (requires Go 1.17+)

Code Examples

  1. Creating a new DNS record:
record := cloudflare.DNSRecord{
    Type:    "A",
    Name:    "example.com",
    Content: "192.0.2.1",
    TTL:     1,
    Proxied: cloudflare.BoolPtr(false),
}

response, err := api.CreateDNSRecord(context.Background(), zoneID, record)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Created DNS record: %+v\n", response)
  1. Listing zones:
zones, err := api.ListZones(context.Background())
if err != nil {
    log.Fatal(err)
}
for _, zone := range zones {
    fmt.Printf("Zone: %s (ID: %s)\n", zone.Name, zone.ID)
}
  1. Purging cache:
purgeEverything := cloudflare.PurgeCacheRequest{
    PurgeEverything: true,
}

resp, err := api.PurgeCache(context.Background(), zoneID, purgeEverything)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Cache purge response: %+v\n", resp)

Getting Started

To start using cloudflare-go, follow these steps:

  1. Install the library:

    go get github.com/cloudflare/cloudflare-go
    
  2. Import the library in your Go code:

    import "github.com/cloudflare/cloudflare-go"
    
  3. Create a new API client:

    api, err := cloudflare.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_API_EMAIL"))
    if err != nil {
        log.Fatal(err)
    }
    
  4. Start using the API methods:

    // Example: List zones
    zones, err := api.ListZones(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    for _, zone := range zones {
        fmt.Printf("Zone: %s\n", zone.Name)
    }
    

Remember to set the CLOUDFLARE_API_KEY and CLOUDFLARE_API_EMAIL environment variables with your Cloudflare API credentials before running your code.

Competitor Comparisons

1,421

DigitalOcean Go API client

Pros of godo

  • More comprehensive documentation and examples
  • Wider range of DigitalOcean services covered
  • Active community and frequent updates

Cons of godo

  • Limited to DigitalOcean services only
  • Slightly more complex API structure
  • Fewer third-party integrations

Code Comparison

godo:

client := godo.NewFromToken("your-api-token")
droplet, _, err := client.Droplets.Create(context.TODO(), &godo.DropletCreateRequest{
    Name:   "example-droplet",
    Region: "nyc3",
    Size:   "s-1vcpu-1gb",
    Image: godo.DropletCreateImage{
        Slug: "ubuntu-20-04-x64",
    },
})

cloudflare-go:

api, err := cloudflare.New("API_KEY", "EMAIL")
zoneID, err := api.ZoneIDByName("example.com")
record := cloudflare.DNSRecord{
    Type:    "A",
    Name:    "example.com",
    Content: "198.51.100.4",
    TTL:     120,
}
resp, err := api.CreateDNSRecord(zoneID, record)

Both libraries provide Go-idiomatic interfaces for interacting with their respective APIs. godo focuses on DigitalOcean's infrastructure services, while cloudflare-go specializes in Cloudflare's DNS and CDN functionalities. The choice between them depends on the specific cloud provider and services required for your project.

AWS SDK for the Go programming language.

Pros of aws-sdk-go

  • More comprehensive coverage of AWS services
  • Larger community and ecosystem
  • Better documentation and examples

Cons of aws-sdk-go

  • Larger and more complex codebase
  • Steeper learning curve for beginners
  • Potentially slower updates for new AWS features

Code Comparison

aws-sdk-go:

svc := s3.New(session.New())
input := &s3.ListObjectsInput{
    Bucket: aws.String("my-bucket"),
}
result, err := svc.ListObjects(input)

cloudflare-go:

api, err := cloudflare.New(apiKey, user)
zoneID, err := api.ZoneIDByName("example.com")
records, err := api.DNSRecords(zoneID, cloudflare.DNSRecord{})

Both SDKs provide idiomatic Go interfaces for interacting with their respective services. aws-sdk-go tends to use more verbose input structs, while cloudflare-go often uses simpler function signatures.

aws-sdk-go is better suited for large-scale AWS projects with complex requirements, while cloudflare-go is more focused and easier to use for Cloudflare-specific tasks. The choice between them depends on the specific needs of your project and which service (AWS or Cloudflare) you're primarily working with.

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:

Pros of azure-sdk-for-go

  • Comprehensive coverage of Azure services
  • Well-documented with extensive examples
  • Regular updates and active maintenance

Cons of azure-sdk-for-go

  • Larger codebase and potentially steeper learning curve
  • More complex setup due to broader scope
  • May include unnecessary dependencies for simple projects

Code Comparison

azure-sdk-for-go:

import (
    "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-03-01/compute"
    "github.com/Azure/go-autorest/autorest/azure/auth"
)

client := compute.NewVirtualMachinesClient(subscriptionID)
client.Authorizer, _ = auth.NewAuthorizerFromEnvironment()

cloudflare-go:

import (
    "github.com/cloudflare/cloudflare-go"
)

api, _ := cloudflare.New(apiKey, user)

The azure-sdk-for-go example demonstrates the setup for a specific service client (Virtual Machines), while cloudflare-go provides a simpler, unified client initialization. Azure's SDK requires more configuration but offers granular control over individual services. Cloudflare's SDK has a more straightforward setup, which can be advantageous for smaller projects or when working with a limited set of Cloudflare services.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

cloudflare-go

Go Reference Test Go Report Card

Note: This library is under active development as we expand it to cover our (expanding!) API. Consider the public API of this package a little unstable as we work towards a v1.0.

A Go library for interacting with Cloudflare's API v4. This library allows you to:

  • Manage and automate changes to your DNS records within Cloudflare
  • Manage and automate changes to your zones (domains) on Cloudflare, including adding new zones to your account
  • List and modify the status of WAF (Web Application Firewall) rules for your zones
  • Fetch Cloudflare's IP ranges for automating your firewall whitelisting

A command-line client, flarectl, is also available as part of this project.

Installation

You need a working Go environment. We officially support only currently supported Go versions according to Go project's release policy.

go get github.com/cloudflare/cloudflare-go

Getting Started

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/cloudflare/cloudflare-go"
)

func main() {
	// Construct a new API object using a global API key
	api, err := cloudflare.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_API_EMAIL"))
	// alternatively, you can use a scoped API token
	// api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
	if err != nil {
		log.Fatal(err)
	}

	// Most API calls require a Context
	ctx := context.Background()

	// Fetch user details on the account
	u, err := api.UserDetails(ctx)
	if err != nil {
		log.Fatal(err)
	}
	// Print user details
	fmt.Println(u)
}

Also refer to the API documentation for how to use this package in-depth.

Experimental improvements

This library is starting to ship with experimental improvements that are not yet ready for production but will be introduced before the next major version. See experimental README for full details.

Contributing

Pull Requests are welcome, but please open an issue (or comment in an existing issue) to discuss any non-trivial changes before submitting code.

License

BSD licensed. See the LICENSE file for details.