Instagram Search users

Search

The Search method has a few overloads. The most simple overload just takes the search query as a parameter. Other overloads lets you customize the search a bit more.

At the time of writing, the method will return up to the 50 first users matching your search query. Unfortunately the Instagram API doesn't support pagination, so you won't be able to fetch more than the 50 first users.

Anyways, here is some example code:

// Make the search
InstagramUsersResponse response = service.Users.Search("bacon");

// Iterate through the user
foreach (InstagramUserSummary user in response.Body.Data) {
                
    // Write something for the console
    Console.WriteLine((user.FullName ?? user.Username) + " (" + user.Id + ")");

}

If you for some reason want to limit the amount of users returned to - lets say 20 - you could use one of the overloaded methods - either by specifying the limit/count as the second parameter:

// Make the search
InstagramUsersResponse response = service.Users.Search("bacon", 20);

// Iterate through the user
foreach (InstagramUserSummary user in response.Body.Data) {
                
    // Write something for the console
    Console.WriteLine((user.FullName ?? user.Username) + " (" + user.Id + ")");

}

Or by specifying an instance of InstagramUserSearchOptions (this is actually what Skybrud.Social uses under the hood).

// Make the search
InstagramUsersResponse response = service.Users.Search(new InstagramUserSearchOptions {
    Query = "bacon",
    Count = 20
});

The class currently only lets you specify the Query and Count properties, so not much extra here than the first two overloads.

Search for a specific user

If you just want to search for a specific user - eg. to find the ID of a user based on his/her username, you can make a search like this:

// Make the search
InstagramUsersResponse response = service.Users.Search(username);

// Find the first user with the specified username
InstagramUserSummary user = response.Body.Data.FirstOrDefault(x => x.Username == username);

From what I can tell, Instagram will always return usernames as lowercase, so you should specify username as lowercase as well (or make a check that isn't case-sensitive).