<titledata-react-helmet="true">Pagination | SpotifyAPI-NET</title><metadata-react-helmet="true"property="og:url"content="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/pagination"><metadata-react-helmet="true"name="docusaurus_locale"content="en"><metadata-react-helmet="true"name="docusaurus_version"content="current"><metadata-react-helmet="true"name="docusaurus_tag"content="docs-default-current"><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."><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/pagination"><linkdata-react-helmet="true"rel="alternate"href="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/pagination"hreflang="en"><linkdata-react-helmet="true"rel="alternate"href="https://johnnycrazy.github.io/SpotifyAPI-NET/docs/pagination"hreflang="x-default"><linkrel="stylesheet"href="/SpotifyAPI-NET/assets/css/styles.834af7f3.css">
<div><ahref="#"class="skipToContent_1oUP">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/SpotifyAPI-NET/"><imgsrc="/SpotifyAPI-NET/img/logo.svg"alt="SpotifyAPI-NET"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/SpotifyAPI-NET/img/logo.svg"alt="SpotifyAPI-NET"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">SpotifyAPI-NET</b></a><divclass="navbar__item dropdown dropdown--hoverable"><aclass="navbar__item navbar__link">Docs</a><ulclass="dropdown__menu"><li><aclass="dropdown__link"href="/SpotifyAPI-NET/docs/introduction">6.X (current)</a></li><li><aclass="dropdown__link"href="/SpotifyAPI-NET/docs/5.1.1/home">5.1.1</a></li></ul></div></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/JohnnyCrazy/SpotifyAPI-NET"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link"><span>GitHub<svgwidth="13.5"height="13.5"aria-hidden="true"viewBox="0 0 24 24"class="iconExternalLink_3J9K"><pathfill="currentColor"d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></span></a><divclass="react-toggle toggle_3Zt9 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><buttonclass="clean-btn backToTopButton_35hR"type="button"title="Scroll to top"><svgviewBox="0 0 24 24"width="28"><pathd="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"fill="currentColor"></path></svg></button><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu thin-scrollbar menu_Bmed menuWithAnnouncementBar_2WvA"><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link menu__link--sublist menu__link--active"href="#">SpotifyAPI-NET</a><ulstyle="display:block;overflow:visible;height:auto"class="menu__list"><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/introduction">Introduction</a></li><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/getting_started">Getting Started</a></li><liclass="menu__list-item"><aclass="menu__link menu__link--sublist menu__link--active"href="#"tabindex="0">Guides</a><ulstyle="display:block;overflow:visible;height:auto"class="menu__list"><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/error_handling">Error Handling</a></li><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/configuration">Configuration</a></li><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/logging">Logging</a></li><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/proxy">Proxy</a></li><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"tabindex="0"href="/SpotifyAPI-NET/docs/pagination">Pagination</a></li><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/retry_handling">Retry Handling</a></li><liclass="menu__list-item"><aclass="menu__link"tabindex="0"href="/SpotifyAPI-NET/docs/iplayableitem">I
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// allPages will include the first page retrived before</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var allPages = await spotify.PaginateAll(page);</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="paginate"></a>Paginate<aclass="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="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">// we need the first page</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var page = await spotify.Playlists.CurrentUsers();</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">await foreach(var item in spotify.Paginate(page))</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Console.WriteLine(item.Name);</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // you can use "break" here!</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></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="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var search = await spotify.Search.Item(new SearchRequest(</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> SearchRequest.Types.All, "Jake"</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">));</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">await foreach(var item in spotify.Paginate(search.Albums, (s) => s.Albums))</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Console.WriteLine(item.Name);</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // you can use "break" here!</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="paginators"></a>Paginators<aclass="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"target="_blank"rel="noopener noreferrer"><code>IPaginator</code></a>, it can be configured how pages are fetched. It can be configured on a global level:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">var config = SpotifyClientConfig</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .CreateDefault()</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> .WithPaginator(new YourCustomPaginator());</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>or on method level:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly csharp"><pretabindex="0"class="prism-code language-csharp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">await foreach(var item in spotify.Paginate(page, new YourCustomPaginator()))</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">{</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Console.WriteLine(item.Name);</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> // you can use "break" here!</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>By default, <ahref="https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI.Web/Clients/SimplePaginator.cs"target="_blank"rel="noopener noreferrer"><code>SimplePaginator</code></a> is used. It fetches pages without any delay.</p></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/JohnnyCrazy/SpotifyAPI-NET/edit/master/SpotifyAPI.Docs/docs/pagination.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><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 lastUpdated_3DPF">Last updated on <b><timedatetime="2022-03-19T19:27:57.000Z">3/19/2022</time></b> by <b>dependabot[bot]</b></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"><aclass="pagination-nav__link"href="/SpotifyAPI-NET/docs/proxy"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagina