RocketAPI for Instagram

FREEMIUM
By RocketAPI | Updated 4 Š“Š½Ń Š½Š°Š·Š°Š“ | Social
Popularity

9.9 / 10

Latency

2,016ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (2)

How to Use Pagination with Instagram API's Endpoints

Pagination in Instagram API is used to divide a large set of data into multiple smaller sets or pages, which can be accessed through the API. This is done to make it easier for users to access and process the data as it is not overwhelming. This also allows users to only receive the data that they need, when they need it.

We will learn how to use pagination with the Get User Media (/instagram/user/get_media) endpoint as an example.

To use pagination with the Get User Media endpoint, you will need to make multiple API requests. On each subsequent request, you will pass the max_id parameter with the value of the next_max_id field from the previous response. This will allow you to retrieve the next set of media.

Here is an example of how you might use pagination with the Get User Media endpoint in Python:

import json
import requests

def get_user_media(user_id, count=12, max_id=None):
  # Set the API endpoint URL
  url = "https://rocketapi-for-instagram.p.rapidapi.com/instagram/user/get_media"
  
  # Set the required headers
  headers = {
    "Content-Type": "application/json",
    "X-RapidAPI-Key": "<your-api-key>",
    "X-RapidAPI-Host": "rocketapi-for-instagram.p.rapidapi.com"
  }
  
  # Set the request data
  data = {
    "id": user_id,
    "count": count,
    "max_id": max_id
  }
  
  # Make the request and get the response
  response = requests.post(url, headers=headers, json=data)
  
  # Return the response as a dictionary
  return json.loads(response.text)


# Create a list to store all the media
media_list = []

# Set the `max_id` parameter to None initially
max_id = None

while True:
	# Make a request to the `Get User Media` endpoint
	response = api.get_user_media(user_id, count=12, max_id=max_id)

	# Add the media from the response to the media list
	media_list += response['items']

	# Update the value of `next_max_id` from the response
	next_max_id = response['next_max_id']

	# If there is no `next_max_id` in the response, then break out of the loop
	if next_max_id is None:
		break
	else:
		max_id = next_max_id

# Print the list of media
print(media_list)

Same example with PHP:

<?php

// Set the API endpoint URL
$url = "https://rocketapi-for-instagram.p.rapidapi.com/instagram/user/get_media";

// Set the required headers
$headers = array(
    "Content-Type: application/json",
    "X-RapidAPI-Key: <your-api-key>",
    "X-RapidAPI-Host: rocketapi-for-instagram.p.rapidapi.com"
);

// Create a list to store all the media
$media_list = array();

// Set the `max_id` parameter to None initially
$max_id = null;

while (true) {
    // Set the request data
    $data = array(
        "id" => $user_id,
        "count" => 12,
        "max_id" => $max_id
    );

    // Make the request and get the response
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    curl_close($ch);

    // Convert the response to a dictionary
    $response = json_decode($response, true);

    // Add the media from the response to the media list
    $media_list = array_merge($media_list, $response['items']);

    // Update the value of `next_max_id` from the response
    $next_max_id = $response['next_max_id'];

    // If there is no `next_max_id` in the response, then break out of the loop
    if ($next_max_id == null) {
        break;
    } else {
        $max_id = $next_max_id;
    }
}

// Print the list of media
print_r($media_list);

In this example, we are using the requests module to make the API request and the json module to parse the response into a dictionary. You will need to provide your own API key and host information, which should be set in the headers variable.

You can then use the get_user_media() method to make requests to the Get User Media endpoint and retrieve the userā€™s media.

We hope youā€™ve enjoyed reading this article. If you have any questions or would like to get in touch, you can reach us at contact@rocketapi.io or connect with us on Telegram at https://t.me/rocketapi. You can also visit our website at https://rocketapi.io for more information about RocketAPI.

If youā€™re looking to build something amazing with custom integration, plans, and more, then look no further. RocketAPI is here to help you with all of your needs.
We look forward to partnering with you!