Back

Step by Step Guide to Building an Ahrefs API Integration in Go

Aug 7, 20247 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your SEO toolset? Let's dive into building an Ahrefs API integration using Go. Ahrefs is a powerhouse for backlink analysis, keyword research, and more. With the ahrefs-go package, we'll make this integration a breeze.

Prerequisites

Before we jump in, make sure you've got:

  • Go installed on your machine
  • An Ahrefs API token (if you don't have one, hop over to Ahrefs and grab it)
  • A basic understanding of Go and APIs (but don't worry, we'll keep it straightforward)

Setting Up the Project

First things first, let's get our project set up:

mkdir ahrefs-integration cd ahrefs-integration go mod init github.com/yourusername/ahrefs-integration go get github.com/ahrefs/ahrefs-go

Great! We've got our module created and the ahrefs-go package installed.

Initializing the Ahrefs Client

Now, let's create our main.go file and set up the Ahrefs client:

package main import ( "fmt" "github.com/ahrefs/ahrefs-go" ) func main() { client := ahrefs.NewClient("YOUR_API_TOKEN") // We'll use this client to make our API calls }

Replace YOUR_API_TOKEN with your actual Ahrefs API token. Easy peasy!

Making API Requests

Ahrefs offers a ton of endpoints, but let's start with something simple - fetching backlink data for a domain:

func main() { client := ahrefs.NewClient("YOUR_API_TOKEN") backlinks, err := client.GetBacklinks("example.com", nil) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Backlinks: %+v\n", backlinks) }

Handling Responses

The ahrefs-go package handles JSON parsing for us, but it's always good to check for errors:

if err != nil { fmt.Printf("Error: %v\n", err) return } // Process your data here for _, link := range backlinks.Backlinks { fmt.Printf("URL: %s, Anchor: %s\n", link.URL, link.Anchor) }

Advanced Usage

Want to handle pagination? No sweat:

params := &ahrefs.GetBacklinksParams{ Limit: 1000, Order: "ahrefs_rank:desc", } for { backlinks, err := client.GetBacklinks("example.com", params) if err != nil { fmt.Printf("Error: %v\n", err) break } // Process backlinks... if len(backlinks.Backlinks) < params.Limit { break // No more pages } params.From = backlinks.Backlinks[len(backlinks.Backlinks)-1].AhrefsRank }

Remember to mind your rate limits! Ahrefs has different limits based on your subscription.

Best Practices

To make the most of your API usage:

  1. Cache results when possible to avoid unnecessary API calls.
  2. Use filters and limits to get only the data you need.
  3. Implement exponential backoff for rate limit errors.

Let's put it all together with a basic backlink analyzer:

package main import ( "fmt" "github.com/ahrefs/ahrefs-go" "sort" ) func main() { client := ahrefs.NewClient("YOUR_API_TOKEN") domain := "example.com" backlinks, err := client.GetBacklinks(domain, &ahrefs.GetBacklinksParams{ Limit: 1000, }) if err != nil { fmt.Printf("Error: %v\n", err) return } anchorTextCount := make(map[string]int) for _, link := range backlinks.Backlinks { anchorTextCount[link.Anchor]++ } type anchorCount struct { Anchor string Count int } var sortedAnchors []anchorCount for anchor, count := range anchorTextCount { sortedAnchors = append(sortedAnchors, anchorCount{anchor, count}) } sort.Slice(sortedAnchors, func(i, j int) bool { return sortedAnchors[i].Count > sortedAnchors[j].Count }) fmt.Printf("Top 10 Anchor Texts for %s:\n", domain) for i, ac := range sortedAnchors[:10] { fmt.Printf("%d. %s (%d occurrences)\n", i+1, ac.Anchor, ac.Count) } }

This script fetches backlinks, counts anchor text occurrences, and displays the top 10 most common anchor texts.

Conclusion

And there you have it! You've just built a solid foundation for your Ahrefs API integration in Go. From here, you can expand to other endpoints, build more complex analyses, or even create a full-fledged SEO tool.

Remember, the ahrefs-go package documentation is your friend for exploring more endpoints and options. Happy coding, and may your SEO insights be ever in your favor!