Instagram

The Social Media Scraping API allows you to scrape information from Instagram. The API uses GraphQL targets which return data in JSON.

πŸ“˜

While the targets in this page use GraphQL under the hood, they are interfaced with via REST.

The following GraphQL targets are available:

Instagram GraphQL User Posts

Returns information about a specific Instagram user.

ParameterTypeRequiredDescriptionExample
targetstringβœ…targetinstagram_graphql_user_posts
querystringquery or user_idInstagram user profile namenba
user_idstringquery or queryUnique Instagram user id56989304580
cursorstringnext page string (end_cursor value in response)QVFBUG5LVkxEaXpPbjZ2T2dhQnVfZTdaUHdnX0VmcXNzek5IRGJTdFdQWkpqOFZDcWRTbXpZSXhkdVRoTlI3d1ExSklzay1sSW1jMkphQVROdnNXazJ6ZA==

Using query vs user_id

The easiest way to use the target is to provide a username as a query:

{
    "target": "instagram_graphql_user_posts",
    "query": "tasty"
}

However, due to Instagram's limitations, using just the query may not work on some usernames. As an escape hatch, you can also provide a user_id to the request.

In order to get the user_id for an account, eg tasty (manual steps):

  1. Navigate to https://www.instagram.com/tasty

  2. Open developer tools and do a global search for "id" in the html. A <script> tag will be highlighted.

  3. Paste the <script> tag into a code editor and search for "id". The id will be nested in one of the profile objects.

This id can then be used in the scraping request:

{
    "target": "instagram_graphql_user_posts",
    "user_id": "56989304580" // user id for account `tasty`
}

Instagram GraphQL profile

Returns information about a specific Instagram user.

ParameterTypeRequiredDescriptionExample
targetstringβœ…targetinstagram_graphql_profile
querystringβœ…Instagram profile namenba

Return value JSON:

  • Basic information (name, description, follower count).
  • 12 most recent posts.

Instagram GraphQL Reel

Returns the number of profiles this user is following (no exact profile handles).

ParameterTypeRequiredDescriptionExample
targetstringβœ…target`instagram_graphql_reel
urlstringβœ…Instagram reel URLhttps://www.instagram.com/reel/ClfPAdQgqTc/

Instagram GraphQL post

Returns information from a specific Instagram post.

ParameterTypeRequiredDescriptionExample
targetstringβœ…targetinstagram_graphql_post
urlstringβœ…Instagram post URLhttps://www.instagram.com/p/ChYHpdAvnob

Create callback for Instagram post

  POST https://scraper-api.smartproxy.com/v2/task

Payload type: JSON

ParameterTypeRequiredDescriptionExamples
targetstringβœ…targetinstagram_graphql_post
urlurlβœ…Instagram post URLhttps://www.instagram.com/p/ChYHpdAvnob

curl -u username:password -X POST --url https://scraper-api.smartproxy.com/v2/task -H "Content-Type: application/json" -d "{\"url\": \"https://www.instagram.com/p/ChYHpdAvnob/\", \"target\": \"instagram_graphql_post\",\"locale\": \"en-us\",\"geo\": \"United States\" }"
import requests

headers = {
    'Content-Type': 'application/json'
}

task_params = {
    'url': 'https://www.instagram.com/p/ChYHpdAvnob/',
  	'target': 'instagram_graphql_post',
  	'locale': 'en-us',
  	'geo': 'United States'
}

username = 'username'
password = 'password'
  
response = requests.post(
    'https://scraper-api.smartproxy.com/v2/task',
    headers = headers,
    json = task_params,
    auth = (username, password)
)
print(response.text)
<?php

$params = array(
    'url' => 'https://www.instagram.com/p/ChYHpdAvnob/',
  	'target' => 'instagram_graphql_post',
  	'locale' => 'en-us',
  	'geo' => 'United States'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://scraper-api.smartproxy.com/v2/task');
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'password');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
echo $result;

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>

Pagination for Posts & Hashtags

Pagination is supported for instagram_graphql_hashtags & instagram_graphql_user_posts targets. First response returns pagination information - existence of multiple pages & end cursor of the current page. Providing this cursor value with a subsequent request will fetch the following page.

Retrieve parsed results via callback

GET https://scraper-api.smartproxy.com/v2/task/{Task_ID}/results?type=parsed

Retrieve raw HTML results via callback

GET https://scraper-api.smartproxy.com/v2/task/{Task_ID}/results?type=raw