Documentation
¶
Overview ¶
Package instagram is a pure-Go, dependency-free, best-effort read client for public Instagram content served through Instagram's web JSON endpoints.
This client is fragile BY NATURE. Instagram does not offer these endpoints as a stable public API: it changes, rate-limits, and locks them frequently, and it may reject unauthenticated requests outright. Reads therefore often require a valid logged-in "sessionid" cookie (see WithSessionID), and any request may break at any time when Instagram changes its endpoints, headers, response shape, or blocking policy. Treat non-2xx responses (particularly 401, 403 and 429) as signals that Instagram is blocking the request rather than as a bug in this library.
The package deliberately hides this fragility behind a small, stable Go API so that callers can depend on the types even as the underlying transport shifts.
It uses only the Go standard library and builds with CGO disabled.
Index ¶
Constants ¶
const DefaultAppID = "936619743392459"
DefaultAppID is the public web app id Instagram's own site sends as the x-ig-app-id header. It is required by the web_profile_info endpoint.
const DefaultBaseURL = "https://www.instagram.com"
DefaultBaseURL is the default Instagram web origin.
const DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
DefaultUserAgent is a browser-like User-Agent used when none is configured.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// BaseURL is the origin requests are sent to. Defaults to DefaultBaseURL.
BaseURL string
// HTTPClient performs requests. Defaults to http.DefaultClient.
HTTPClient *http.Client
// UserAgent is sent as the User-Agent header.
UserAgent string
// SessionID, when set, is sent as the "sessionid" cookie to authenticate
// reads. Instagram frequently requires this for public data.
SessionID string
// CSRFToken, when set, is sent as the "csrftoken" cookie and the X-CSRFToken
// header. The private friendships endpoints (see [Client.Following]) require
// it in addition to the sessionid.
CSRFToken string
// AppID is sent as the x-ig-app-id header. Defaults to DefaultAppID.
AppID string
}
Client is a best-effort Instagram web read client. Construct it with New.
func (*Client) CurrentUserID ¶ added in v0.2.0
CurrentUserID returns the logged-in user's numeric id, read from the private /api/v1/accounts/current_user/ endpoint. It requires a valid sessionid (see WithSessionID); without one Instagram answers 302→login or 4xx, surfaced as an error. The returned id is what Client.Following needs as its userID.
func (*Client) Following ¶ added in v0.2.0
Following returns one page of the accounts userID follows, starting at maxID ("" for the first page). It requests GET {BaseURL}/api/v1/friendships/<userID>/following/ with the x-ig-app-id header and the sessionid (and, when configured, csrftoken) cookie — the same authentication the private web app sends. Page through the whole list by passing the previous page's NextMaxID until it comes back "".
This endpoint is private and Instagram gates it aggressively: without a valid sessionid it answers 302→login or 401/403/429, all surfaced here as errors.
func (*Client) UserProfile ¶
UserProfile fetches a public profile and its recent posts.
It requests GET {BaseURL}/api/v1/users/web_profile_info/?username=<u> with the x-ig-app-id header (and the sessionid cookie when configured). This endpoint is undocumented and may require authentication; see the package documentation for the fragility caveats.
type FollowedUser ¶ added in v0.2.0
type FollowedUser struct {
PK string // the account's numeric id (as a string)
Username string // the @handle, without the "@"
FullName string // the display name ("" when the account sets none)
}
FollowedUser is one account the logged-in user follows, as returned by the friendships following list.
type FollowingPage ¶ added in v0.2.0
type FollowingPage struct {
Users []FollowedUser
NextMaxID string
}
FollowingPage is one page of the logged-in user's following list. NextMaxID is the cursor for the following page, and is "" when there are no more pages.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBaseURL ¶
WithBaseURL overrides the request origin (useful for testing).
func WithCSRFToken ¶ added in v0.2.0
WithCSRFToken sets the "csrftoken" cookie / X-CSRFToken header sent with requests to the private friendships endpoints (see Client.Following).
func WithHTTPClient ¶
WithHTTPClient sets the http.Client used for requests.
func WithSessionID ¶
WithSessionID sets the "sessionid" cookie sent with requests.
type Post ¶
type Post struct {
ID string
Shortcode string
Caption string
Owner string // username
Permalink string // https://www.instagram.com/p/<shortcode>/
DisplayURL string // main image
IsVideo bool
VideoURL string
Likes int
Comments int
Timestamp time.Time
}
Post is a single piece of timeline media on a profile.
