How do I get posts or reels from a public Instagram account via API?
Posts: GET /instagram/profile/posts with the account's username (or user_id) returns its latest photos, videos and carousels with captions and permalinks. Reels: GET /instagram/profile/reels with the account's user_id. Both page with end_cursor.
How to do it with SocialAPIs
Get the user_id (the long one, e.g. 17841401474538262) from /instagram/user/id with the username or profile link; the shorter pk it also returns is for highlights, not for posts or reels. Price per page of results: posts 1 credit, reels 1 credit.
One post by its link: /instagram/post/details with the post's shortcode (the part after /p/ in the URL).
Endpoints
GET /instagram/profile/posts1 credit
Get posts from a user profile with pagination.
Params:
username,user_id,end_cursorInstagram Profile Posts: docs and playgroundMCP:
instagram_profile→postsGET /instagram/profile/reels1 credit
Get reels from a user profile with pagination.
Params:
user_id(required),end_cursorInstagram Profile Reels: docs and playgroundMCP:
instagram_profile→reelsGET /instagram/user/id1 credit
Get the unique ID of any Instagram profile from link or username.
Params:
link,usernameInstagram User ID: docs and playgroundMCP:
instagram_profile→idGET /instagram/post/details1 credit
Get details of a specific Instagram post.
Params:
shortcode(required)Instagram Post Details: docs and playgroundMCP:
instagram_post→details
curl -G "https://api.socialapis.io/instagram/profile/posts" \
--max-time 100 \
--fail-with-body \
-H "x-api-token: $SOCIALAPIS_API_KEY" \
--data-urlencode "username=nasa"import os
import requests
response = requests.get(
"https://api.socialapis.io/instagram/profile/posts",
headers={"x-api-token": os.environ["SOCIALAPIS_API_KEY"]},
params={
"username": "nasa",
},
timeout=100,
)
response.raise_for_status()
print(response.json())// Node 18+, as an ES module (e.g. main.mjs)
async function get(url, params) {
const query = new URLSearchParams(params).toString();
const response = await fetch(query ? `${url}?${query}` : url, {
headers: { "x-api-token": process.env.SOCIALAPIS_API_KEY },
signal: AbortSignal.timeout(100000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
return response.json();
}
const data = await get("https://api.socialapis.io/instagram/profile/posts", {
username: "nasa",
});
console.log(data);import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
private static final HttpClient CLIENT = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
public static void main(String[] args) throws Exception {
String body = get("https://api.socialapis.io/instagram/profile/posts", Map.of(
"username", "nasa"));
System.out.println(body);
}
static String get(String url, Map<String, String> params) throws Exception {
String query = params.entrySet().stream()
.map(e -> URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8) + "="
+ URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));
HttpRequest request = HttpRequest.newBuilder(URI.create(query.isEmpty() ? url : url + "?" + query))
.header("x-api-token", System.getenv("SOCIALAPIS_API_KEY"))
.timeout(Duration.ofSeconds(100))
.GET()
.build();
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2) {
throw new RuntimeException("HTTP " + response.statusCode() + ": " + response.body());
}
return response.body();
}
}package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"time"
)
func main() {
body, err := get("https://api.socialapis.io/instagram/profile/posts", url.Values{
"username": {"nasa"},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
func get(endpoint string, params url.Values) ([]byte, error) {
if len(params) > 0 {
endpoint += "?" + params.Encode()
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("x-api-token", os.Getenv("SOCIALAPIS_API_KEY"))
client := &http.Client{Timeout: 100 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
}
return body, nil
}# pip install socialapis-sdk
import os
from socialapis import Instagram
client = Instagram(api_token=os.environ["SOCIALAPIS_API_KEY"])
response = client.get_profile_posts("nasa")
print(response)// npm install socialapis-sdk
import { Instagram } from "socialapis-sdk";
const client = new Instagram({ apiToken: process.env.SOCIALAPIS_API_KEY });
const response = await client.getProfilePosts("nasa");
console.log(response);Your API key goes in the x-api-token header (here from the SOCIALAPIS_API_KEY environment variable). Get a free key.
Good to know
- Each post has
shortcode,permalink,caption,is_video,is_carouselanddisplay_url; the next page's cursor is inpage_info.end_cursor. - Reels include play counts and audio details.
- Public accounts only.