Go setup
Requirements
To use the UpRock API with Go, you need Go 1.18 or higher and Go modules enabled (default in Go 1.16+).
Initialize Go Module
If starting a new project, initialize a Go module:
go mod init your-module-nameReplace your-module-name with your actual module path (e.g., github.com/yourusername/projectname).
Go modules are the standard way to manage dependencies in Go projects. They track your project's dependencies in go.mod and go.sum files.
Install Required Dependencies
go get github.com/joho/godotenvDependencies:
godotenv- Loads environment variables from.envfiles
The standard library net/http package is used for HTTP requests, so no additional HTTP client is needed.
Configure Environment Variables
Ensure you have set up your environment variables as described in the get your API keys guide. Your .env file should contain:
UPROCK_API_KEY=your-api-key-here
# UPROCK_BASE_URL is optional - defaults to https://edge.uprock.comNever commit your .env file to version control. Add it to .gitignore.
Verify Your Setup
Create a file named main.go and run it to verify your setup:
package main
import (
"fmt"
"net/http"
"os"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from .env file
_ = godotenv.Load()
// Get configuration
apiKey := os.Getenv("UPROCK_API_KEY")
baseURL := os.Getenv("UPROCK_BASE_URL")
if baseURL == "" {
baseURL = "https://edge.uprock.com"
}
// Validate API key is set
if apiKey == "" {
fmt.Println("Error: UPROCK_API_KEY not set")
os.Exit(1)
}
// Create HTTP request
req, err := http.NewRequest("GET", baseURL+"/crawl/v1/health", nil)
if err != nil {
fmt.Println("Error creating request:", err)
os.Exit(1)
}
// Set authorization header
req.Header.Set("Authorization", "Bearer "+apiKey)
// Execute request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request error:", err)
os.Exit(1)
}
defer resp.Body.Close()
// Display result
if resp.StatusCode == 200 {
fmt.Println("✓ Setup verified - Ready to use UpRock Crawl API")
} else {
fmt.Printf("✗ Setup failed with status code: %d\n", resp.StatusCode)
os.Exit(1)
}
}Expected Output
✓ Setup verified - Ready to use UpRock Crawl APICommon failure codes:
401- Invalid or missing API key403- Inactive plan or insufficient permissions404- Incorrect base URL
Troubleshooting
Below are solutions to common setup issues. If these don't resolve your problem, reach out to support@uprock.com.
API Key Not Found
Verify your .env file is in the project root directory and the variable name is exactly UPROCK_API_KEY.
Build Errors
Ensure all dependencies are downloaded:
go mod tidy
go mod verifyConnection Errors
If behind a corporate proxy, configure the HTTPS_PROXY environment variable as described in the get your API keys guide.