Instagram Get a list of recent media

Get a list of recent media

The users endpoint allows you to get information about the authenticated user (when using an access token) or for a specific user. Both of these methods are described below:

Get recent media for the authenticated user

The GetRecentMedia method will let you get the recent media of either the authenticated user or a another user depending on the parameters.

If you just want to get the most recent media for the authenticated user, you don't have to specify any parameters for the method.

Notice that this requires a user context.

// Make the call to the Instagram API
InstagramRecentMediaResponse response = service.Users.GetRecentMedia();

// Iterate through the returned media
foreach (InstagramMedia media in response.Body.Data) {

    Console.WriteLine(media.Link);

}

The Data property is an array of instances of InstagramMedia. This class serves as a parent class, so since a media can by either an image or a video, the items in the array can may be either an instance of InstagramImage or InstagramVideo (since both classes inherit from InstagramMedia).

That means that both InstagramImage and InstagramVideo will inherit properties from the InstagramMedia class. The InstagramImage class doesn't introduce any new properties, but the InstagramVideo class has an extra property for specifying URLs of the video.

See the InstagramMedia class on GitHub → See the InstagramImage class on GitHub → See the InstagramVideo class on GitHub →

Get recent media for a specific user

If you want to get the most recent media of another user, that is possible as well - you can do that by specifying the ID of the user as the first parameter for the GetRecentMedia method. It is not possible to use the user's username directly - for that to work you first have to search for the user, and then grab the ID of the user from the search results.

// Make the call to the Instagram API
InstagramRecentMediaResponse response = service.Users.GetRecentMedia(189413627);

// Iterate through the returned media
foreach (InstagramMedia media in response.Body.Data) {

    Console.WriteLine(media.Link);

}

Pagination and other options

The list of media returned by the Instagram API is paginated, so you will only get a subset of the media the user has posted. If you wish to get media beyond the first page, you can use the InstagramUserRecentMediaOptions class. The class has the following properties:

Name Type Description
Count int Gets or sets the maximum amount of media to be returned.
MaxTimestamp DateTime? Gets or sets the maximum timestamp for the search. Only media before this timestamp is returned.
MinTimestamp DateTime? Gets or sets the minimum timestamp for the search. Only media after this timestamp is returned.
MinId string Gets or sets the minimum media ID for the search. Only media after this ID is returned.
MaxId string Gets or sets the maximum media ID for the search. Only media before this ID is returned.

See the InstagramUserRecentMediaOptions class on GitHub →

You can do pagination based on either timestamps or IDs, but generally using IDs is the most reliable approach since multiple media may share the same timestamp.

// Temporary list for storing the retrieved media
List<InstagramMedia> temp = new List<InstagramMedia>();

// Declare the initial search options
InstagramUserRecentMediaOptions options = new InstagramUserRecentMediaOptions {
    Count = 5,
};

// Make the call to the Instagram API
InstagramRecentMediaResponse response = service.Users.GetRecentMedia(options);

// Add the media to the list
temp.AddRange(response.Body.Data);

// No reason to make another request if the first request didn't return any media
if (temp.Count > 0) {

    // Update the options
    options.MaxId = temp.Last().Id;

    // Make another call to the Instagram API
    response = service.Users.GetRecentMedia(options);

    // Add the media to the list
    temp.AddRange(response.Body.Data);

}