<titledata-react-helmet="true">Pagination | SpotifyAPI-NET</title><metadata-react-helmet="true"name="docsearch:version"content="next"><metadata-react-helmet="true"name="twitter:card"content="summary_large_image"><metadata-react-helmet="true"property="og:title"content="Pagination | SpotifyAPI-NET"><metadata-react-helmet="true"name="description"content="When working with Spotify responses, you will often encounter the Paging type."><metadata-react-helmet="true"property="og:description"content="When working with Spotify responses, you will often encounter the Paging type."><metadata-react-helmet="true"property="og:url"content="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/next/pagination"><linkdata-react-helmet="true"rel="shortcut icon"href="/SpotifyAPI-NET/img/favicon.ico"><linkdata-react-helmet="true"rel="canonical"href="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/next/pagination"><linkrel="stylesheet"href="/SpotifyAPI-NET/styles.8a053330.css">
</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// allPages will include the first page retrived before</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var allPages = await spotify.PaginateAll(page);</span></div></div></div></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_ZqCz"id="paginate"></a>Paginate<aaria-hidden="true"tabindex="-1"class="hash-link"href="#paginate"title="Direct link to heading">#</a></h2><divclass="admonition admonition-info alert alert--info"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>.NET Standard >= 2.1 required</h5></div><divclass="admonition-content"></div></div><p><code>Paginate</code> is based on <code>IAsyncEnumerable</code> and streams pages instead of returning them all in one list. This allows it to break the fetching early and keep only 1 page in memory at a time. This method should always be preferred to <code>PaginateAll</code>.</p><divclass="mdxCodeBlock_iHAB"><divclass="codeBlockContent_32p_"><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_1BYj">Copy</button><divtabindex="0"class="prism-code language-csharp codeBlock_19pQ"><divclass="codeBlockLines_2n9r"style="color:#bfc7d5;background-color:#292d3e"><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// we need the first page</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var page = await spotify.Playlists.CurrentUsers();</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">await foreach(var item in spotify.Paginate(page))</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Console.WriteLine(item.Name);</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // you can use "break" here!</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></div></div></div></div></div><p>Some endpoints have nested and/or multiple paginations objects. When requesting the next page, it will not return the actual paging object but rather the root level endpoint object. A good example is the <code>Search</code> endpoint, which contains up to 5 Paging objects. Requesting the next page of the nested <code>Artists</code> paging object will return another <code>Search</code> response, instead of just <code>Artists</code>. You will need to supply a mapper function to the <code>Paginate</code> call, which returns the correct paging object:</p><divclass="mdxCodeBlock_iHAB"><divclass="codeBlockContent_32p_"><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_1BYj">Copy</button><divtabindex="0"class="prism-code language-csharp codeBlock_19pQ"><divclass="codeBlockLines_2n9r"style="color:#bfc7d5;background-color:#292d3e"><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var search = await spotify.Search.Item(new SearchRequest(</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> SearchRequest.Types.All, "Jake"</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">));</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">await foreach(var item in spotify.Paginate(search.Albums, (s) => s.Albums))</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Console.WriteLine(item.Name);</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // you can use "break" here!</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></div></div></div></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_ZqCz"id="paginators"></a>Paginators<aaria-hidden="true"tabindex="-1"class="hash-link"href="#paginators"title="Direct link to heading">#</a></h2><p>Via the interface <ahref="https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI.Web/Clients/Interfaces/IPaginator.cs"><code>IPaginator</code></a>, it can be configured how pages are fetched. It can be configured on a global level:</p><divclass="mdxCodeBlock_iHAB"><divclass="codeBlockContent_32p_"><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_1BYj">Copy</button><divtabindex="0"class="prism-code language-csharp codeBlock_19pQ"><divclass="codeBlockLines_2n9r"style="color:#bfc7d5;background-color:#292d3e"><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var config = SpotifyClientConfig</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .CreateDefault()</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .WithPaginator(new YourCustomPaginator());</span></div></div></div></div></div><p>or on method level:</p><divclass="mdxCodeBlock_iHAB"><divclass="codeBlockContent_32p_"><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_1BYj">Copy</button><divtabindex="0"class="prism-code language-csharp codeBlock_19pQ"><divclass="codeBlockLines_2n9r"style="color:#bfc7d5;background-color:#292d3e"><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">await foreach(var item in spotify.Paginate(page, new YourCustomPaginator()))</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Console.WriteLine(item.Name);</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // you can use "break" here!</span></div><divclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></div></div></div></div></div><p>By default, <ahref="https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI.Web/Clients/SimplePaginator.cs"><code>SimplePaginator</code></a> is used. It fetches pages without any delay.</p></div></article><divclass="margin-vert--xl"><divclass="row"><divclass="col"><ahref="https://github.com/JohnnyCrazy/SpotifyAPI-NET/edit/master/SpotifyAPI.Docs/docs/pagination.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="1.2em"width="1.2em"preserveAspectRatio="xMidYMid meet"viewBox="0 0 40 40"style="margin-right:0.3em;vertical-align:sub"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col text--right"><em><small>Last updated on <timedatetime="2021-03-20T17:09:58.000Z"class="docLastUpdatedAt_1sqk">3/20/2021</time> by <strong>dependabot[bot]</strong></small></em></div></div></div><divclass="margin-vert--lg"><navclass="pagination-nav"><divclass="pagination-nav__item"><aclass="pagination-nav__link"href="/SpotifyAPI-NET/docs/next/proxy"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">« Proxy</div></a></div><divclass="pagination-nav__item pagination-nav__item--next"><aclass="pagination-nav__link"href="/SpotifyAPI-NET/docs/next/retry