Instagram API for Go
Instagram API for Go Developers
HikerAPI is a REST API that works with Go's standard net/http package. No external dependencies required.
Quick Start
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const (
apiKey = "your_access_key"
baseURL = "https://api.hikerapi.com"
)
type User struct {
PK int64 `json:"pk"`
Username string `json:"username"`
FullName string `json:"full_name"`
FollowerCount int `json:"follower_count"`
MediaCount int `json:"media_count"`
}
func getUser(username string) (*User, error) {
params := url.Values{"username": {username}}
req, _ := http.NewRequest("GET",
baseURL+"/v1/user/by/username?"+params.Encode(), nil)
req.Header.Set("x-access-key", apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var user User
json.NewDecoder(resp.Body).Decode(&user)
return &user, nil
}
func main() {
user, _ := getUser("instagram")
fmt.Printf("%s: %d followers\n", user.Username, user.FollowerCount)
}
Concurrent Requests
func getUsers(usernames []string) []*User {
ch := make(chan *User, len(usernames))
for _, u := range usernames {
go func(username string) {
user, _ := getUser(username)
ch <- user
}(u)
}
users := make([]*User, 0, len(usernames))
for range usernames {
users = append(users, <-ch)
}
return users
}
FAQ
Do I need any external packages?
No. Standard library net/http and encoding/json are sufficient.