
API pagination broke differently on each endpoint
API pagination broke differently on each endpoint Started building a dashboard that pulls data from a client's API. Three endpoints, all paginated. Should be simple right? Nope. First endpoint worked fine Their /users endpoint used offset pagination. Standard stuff. Pass ?offset=0&limit=100 , get 100 results, increment offset by 100, repeat until you get less than 100 back. def fetch_users ( api_key ): offset = 0 limit = 100 all_users = [] while True : response = requests . get ( f " https://api.example.com/users " , params = { " offset " : offset , " limit " : limit }, headers = { " Authorization " : f " Bearer { api_key } " } ) data = response . json () if len ( data ) < limit : all_users . extend ( data ) break all_users . extend ( data ) offset += limit return all_users Worked first try. Got 847 users. Moved on. Second endpoint used cursor tokens Their /orders endpoint didn't use offsets. Used cursor tokens instead. You get a next_cursor in the response, pass it back in the next re
Continue reading on Dev.to Python
Opens in a new tab



