Skip to main content

Go

Go

Install the official Go SDK or use Go's standard net/http package directly. All examples below are copy-paste ready.

Install SDK

BASH
go get github.com/xcrop-io/xcrop-go

Helper Function (without SDK)

xcrop.goGO
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
)

var (
	apiKey = os.Getenv("XCROP_API_KEY")
	base   = "https://xcrop.io/api"
)

func xcropGet(path string) (map[string]interface{}, error) {
	req, _ := http.NewRequest("GET", base+path, nil)
	req.Header.Set("Authorization", "Bearer "+apiKey)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	var result map[string]interface{}
	json.Unmarshal(body, &result)
	return result, nil
}

func xcropPost(path string, data interface{}) (map[string]interface{}, error) {
	b, _ := json.Marshal(data)
	req, _ := http.NewRequest("POST", base+path, bytes.NewReader(b))
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	var result map[string]interface{}
	json.Unmarshal(body, &result)
	return result, nil
}

Search Tweets

GO
func main() {
	result, _ := xcropPost("/v2/search", map[string]interface{}{
		"query": "#bitcoin",
		"limit": 20,
		"sort":  "latest",
	})

	tweets := result["data"].([]interface{})
	for _, t := range tweets {
		tweet := t.(map[string]interface{})
		author := tweet["author"].(map[string]interface{})
		fmt.Printf("@%s: %s\n", author["username"], tweet["text"])
	}
}

User Profile

GO
result, _ := xcropGet("/v2/users/elonmusk")
user := result["data"].(map[string]interface{})
fmt.Printf("%s — %.0f followers\n", user["name"], user["followers"])

Create a Tweet

GO
result, _ := xcropPost("/v2/tweets/create", map[string]interface{}{
	"text": "Hello from XCROP API!",
})
data := result["data"].(map[string]interface{})
fmt.Println("Posted:", data["url"])

More in Code Examples

Full documentation with search: xcrop.io/docs