diff --git a/examples/fetch_allowed_domains.py b/examples/fetch_allowed_domains.py new file mode 100644 index 00000000..963530b5 --- /dev/null +++ b/examples/fetch_allowed_domains.py @@ -0,0 +1,24 @@ +from examples import bb +from browserbase import BadRequestError + + +def run() -> None: + # A host on the list (or a subdomain of one) is fetched as usual. + page = bb.fetch_api.create( + url="https://example.com/", + allowed_domains=["example.com"], + ) + print(f"Fetched {page.status_code} from example.com") + + # Any other host, or a redirect to one, is rejected with a 400 before it is reached. + try: + bb.fetch_api.create( + url="https://example.com/", + allowed_domains=["example.org"], + ) + except BadRequestError as error: + print(f"Blocked: {error.message}") + + +if __name__ == "__main__": + run()