Skip to content

next_page() / yield_pages() send the client's API key instead of the one passed to search() #57

Backlog

Description

@Divyanshu-2907

When the key is passed per call, page 1 uses it but every later page uses the client's own key. With a keyless client, page 2 has no key and fails with a 401.

client = serpapi.Client(api_key="TEAM_A")
results = client.search(engine="google", q="coffee", api_key="USER_B")
for page in results.yield_pages(max_pages=2):
    ...  # page 1 sends USER_B, page 2 sends TEAM_A

Same with serpapi.search(api_key=...): page 2 goes out without a key.

As the comment in next_page() says, the next-page URL doesn't include the key, so it adds self.client.api_key. That key isn't always the one the search used. The async next_page() has the same problem.

Related to #46: the review there flags next_page() dropping the session key on a keyless client. This is a different case in the same spot: a key passed per call to search() isn't reused for later pages, even on master. Since #46 touches next_page(), it may be easiest to cover both there. Otherwise I can open a separate PR once #46 lands.

I have a fix with offline tests for both clients (results remember the key sent with their request, and fall back to the client key otherwise), happy to share it either way.

Activity

  1. hilmanski commented on Dec 1, 2026

    @hilmanski
    Contributor

    Thanks for opening an issue. We'll investigate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions