### Query Brave Search AI Summarizer with Python OpenAI SDK Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/get-started This Python example demonstrates how to use the OpenAI SDK to send a direct query to Brave Search's AI summarizer endpoint. It requires the `openai` library installed and a valid Brave Search API key. The code sends a user prompt and prints the AI-generated summarized response. ```python from openai import OpenAI client = OpenAI( api_key="", base_url="https://api.search.brave.com/res/v1", ) completions = client.chat.completions.create( messages=[ { "role": "user", "content": "What are the best things to do in Paris with kids?", } ], model="brave-pro", stream=False, ) print(completions.choices[0].message.content) ``` -------------------------------- ### CURL Example for Brave Spellcheck API Query Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/spellcheck/get-started This example demonstrates how to make a basic GET request to the Brave Spellcheck API using CURL. It includes specifying a query parameter ('q'), country, and essential request headers for accepting JSON and providing the API key. ```CURL curl -s --compressed "https://api.search.brave.com/res/v1/spellcheck/search?q=helo&country=US" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Set Up Project Structure for Image Search Guide Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This snippet creates the necessary directory structure for the image search application, including a 'public' folder for static assets. It prepares the environment for both server-side and client-side files, ensuring a clean project setup. ```Shell mkdir img-search && cd img-search mkdir public touch public/images.html touch public/styles.css ``` -------------------------------- ### Install Node.js Express Dependency for Server Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This command installs Express.js, a minimal and flexible Node.js web application framework, which is used to build the server for handling API calls. It is the sole dependency for this project, simplifying setup. ```Shell npm install express --save ``` -------------------------------- ### Example JSON Response with Summarizer Key Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/get-started This JSON snippet illustrates the structure of a response from the Web Search API when a query is eligible for summarization. It contains the 'summarizer' object, which includes a 'key' that is essential for making subsequent requests to the summarizer endpoint to retrieve the actual summarized content. ```json { "summarizer": { "type": "summarizer", "key": "{\"query\": \"what is the second highest mountain\", \"country\": \"us\", \"language\": \"en\", \"safesearch\": \"moderate\", \"results_hash\": \"a51e129180225a2f4fe1a00984bcbf58f0ae0625c97723aae43c2c6e3440715b}" } } ``` -------------------------------- ### Execute Node.js Server for Image Search Client Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This command initiates the Node.js server by running the `index.js` file, which is essential for the image search client to function. It starts the local web server, typically on port 4000, allowing access to the client application via a web browser. Ensure Node.js is installed and `index.js` is present in the current directory. ```Shell node index.js ``` -------------------------------- ### Install Python Dependencies for Brave Search API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/code-samples This command installs the required Python libraries, `aiohttp` for asynchronous HTTP requests and `aiolimiter` for rate limiting, which are essential for interacting with the Brave Search API. Ensure you have Python 3 installed on your machine before running this command. ```bash pip install aiohttp aiolimiter ``` -------------------------------- ### Example JSON Response with Brave Summarizer Key Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search This JSON snippet illustrates the 'summarizer' object returned within the Web Search API response when a query is eligible for summarization. The 'key' field within this object is essential for subsequent requests to the summarizer endpoint to retrieve the actual summarized content. ```JSON { "summarizer": { "type": "summarizer", "key": "{\"query\": \"what is the second highest mountain\", \"country\": \"us\", \"language\": \"en\", \"safesearch\": \"moderate\", \"results_hash\": \"a51e129180225a2f4fe1a00984bcbf58f0ae0625c97723aae43c2c6e3440715b}" } } ``` -------------------------------- ### Perform Initial Web Search with Brave API for Summary Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search This CURL command demonstrates how to initiate a web search request to the Brave API, including the 'summary=1' parameter to indicate eligibility for summarization. It specifies required headers like 'Accept', 'Accept-Encoding', and 'X-Subscription-Token' for authentication and proper response handling. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=what+is+the+second+highest+mountain&summary=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Integrate Brave Search API with OpenAI SDK in Python Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search This Python snippet demonstrates how to use the Brave Search API's OpenAI-compatible endpoint (`/res/v1/chat/completions`) to get AI-powered answers. It requires the `openai` Python library and a Brave Search API key. The code sends a user query and prints the AI-generated response, leveraging Brave Search for web context. ```Python from openai import OpenAI client = OpenAI( api_key="", base_url="https://api.search.brave.com/res/v1", ) completions = client.chat.completions.create( messages=[ { "role": "user", "content": "What are the best things to do in Paris with kids?", } ], model="brave-pro", stream=False, ) print(completions.choices[0].message.content) ``` -------------------------------- ### Perform Initial Web Search for Summary with cURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/get-started This cURL command demonstrates how to make an initial request to the Brave Web Search API endpoint. It includes a query and the 'summary=1' parameter to indicate eligibility for summarization, and requires an API key for authentication. The response will contain a summarizer key if the query is eligible. ```shell curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=what+is+the+second+highest+mountain&summary=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Example cURL Request for Brave Suggest API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/suggest This example demonstrates how to make a GET request to the Brave Suggest API using cURL. It includes common query parameters like 'q' for the search term, 'country' for region, and 'count' for the number of suggestions. Ensure to replace with your valid subscription token. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/suggest/search?q=hello&country=US&count=5" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Perform a Web Search with CURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search/get-started This example demonstrates how to make a basic GET request to the Brave Web Search API using CURL. It includes necessary headers for accepting JSON and GZIP encoding, and requires an API key for authentication. Replace with your actual subscription token. ```bash curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=brave+search" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Brave Spellcheck API Endpoint Reference Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/spellcheck/get-started This snippet provides the base URL for accessing the Brave Spellcheck API, which is used to perform spell checking on a given query. All spellcheck requests should be directed to this endpoint. ```APIDOC https://api.search.brave.com/res/v1/spellcheck/search ``` -------------------------------- ### Execute Brave News Search API Request with cURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/news-search/get-started This example demonstrates how to make a GET request to the Brave News Search API using cURL. It includes common query parameters such as 'q' for the search query, 'count', 'country', 'search_lang', and 'spellcheck', along with essential headers for content negotiation and authentication. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/news/search?q=munich&count=10&country=us&search_lang=en&spellcheck=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Poll Brave Summarizer Endpoint with cURL and Key Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/get-started This cURL command demonstrates how to use the 'key' obtained from the initial Web Search API response to poll the Brave Summarizer API endpoint for the summarized content. Requests to this specific summarizer endpoint are not counted towards your plan, making it cost-effective for retrieving summaries. ```shell curl -s --compressed "https://api.search.brave.com/res/v1/summarizer/search?key=%7B%22query%22%3A%22what%20is%20the%20second%20highest%20mountain%22%2C%22country%22%3A%22us%22%2C%22language%22%3A%22en%22%2C%22safesearch%22%3A%22moderate%22%2C%22results_hash%22%3A%22a51e129180225a2f4fe1a00984bcbf58f0ae0625c97723aae43c2c6e3440715b%22%7D&entity_info=1" \ -H "accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "x-subscription-token: " ``` -------------------------------- ### Brave Summarizer Search API Endpoints Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/get-started This snippet lists the various API endpoints available for the Brave Summarizer Search API. These endpoints are used to access specific types of data or functionalities related to summarization and search, allowing developers to target specific API capabilities. ```APIDOC https://api.search.brave.com/res/v1/web/search https://api.search.brave.com/res/v1/summarizer/search https://api.search.brave.com/res/v1/summarizer/summary https://api.search.brave.com/res/v1/summarizer/summary_streaming https://api.search.brave.com/res/v1/summarizer/title https://api.search.brave.com/res/v1/summarizer/enrichments https://api.search.brave.com/res/v1/summarizer/followups https://api.search.brave.com/res/v1/summarizer/entity_info https://api.search.brave.com/res/v1/chat/completions ``` -------------------------------- ### Poll Brave Summarizer Endpoint Using Retrieved Key Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search This CURL command demonstrates how to use the 'key' obtained from the initial web search response to poll the Brave Summarizer API for the summarized content. This subsequent request is not billed and retrieves the final summarized results based on the provided key. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/summarizer/search?key=%7B%22query%22%3A%22what%20is%20the%20second%20highest%20mountain%22%2C%22country%22%3A%22us%22%2C%22language%22%3A%22en%22%2C%22safesearch%22%3A%22moderate%22%2C%22results_hash%22%3A%22a51e129180225a2f4fe1a00984bcbf58f0ae0625c97723aae43c2c6e3440715b%22%7D&entity_info=1" \ -H "accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "x-subscription-token: " ``` -------------------------------- ### Perform Image Search with cURL and Brave API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/get-started This cURL example demonstrates how to send a GET request to the Brave Image Search API. It showcases the use of various query parameters such as 'q' for the search term, 'safesearch' for content filtering, and 'count' for result limits, along with required 'Accept' and 'X-Subscription-Token' headers. Remember to replace '' with your actual API key for successful authentication. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/images/search?q=munich&safesearch=strict&count=20&search_lang=en&country=us&spellcheck=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Brave Summarizer Search API Available Endpoints Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search This section lists the various REST API endpoints provided by Brave Summarizer Search API. These endpoints facilitate different types of data retrieval, including general web search, specific summarization tasks, streaming summaries, and chat completions. ```APIDOC https://api.search.brave.com/res/v1/web/search https://api.search.brave.com/res/v1/summarizer/search https://api.search.brave.com/res/v1/summarizer/summary https://api.search.brave.com/res/v1/summarizer/summary_streaming https://api.search.brave.com/res/v1/summarizer/title https://api.search.brave.com/res/v1/summarizer/enrichments https://api.search.brave.com/res/v1/summarizer/followups https://api.search.brave.com/res/v1/summarizer/entity_info https://api.search.brave.com/res/v1/chat/completions ``` -------------------------------- ### Example cURL Request for Brave Video Search API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/video-search This cURL command demonstrates how to make a basic GET request to the Brave Video Search API. It includes common query parameters such as search term (q), result count, country, search language, and spellcheck, along with required headers for content type, encoding, and API key. ```shell curl -s --compressed "https://api.search.brave.com/res/v1/videos/search?q=munich&count=10&country=us&search_lang=en&spellcheck=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Perform a Basic Web Search with cURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search This example demonstrates how to make a basic GET request to the Brave Web Search API using cURL. It queries for 'brave search' and specifies "Accept: application/json" and "Accept-Encoding: gzip" headers, along with a placeholder for the API key. Replace with your actual subscription token. ```bash curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=brave+search" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Example cURL Request for Brave Video Search API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/video-search/get-started An example cURL command to query the Brave Video Search API for videos related to 'munich'. This request limits results to 10, specifies the country as US, sets the search language to English, and enables spellcheck. It includes necessary headers for JSON acceptance, gzip encoding, and API key authentication. ```bash curl -s --compressed "https://api.search.brave.com/res/v1/videos/search?q=munich&count=10&country=us&search_lang=en&spellcheck=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Python Brave Search Summarizer API Client Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/code-samples This Python script demonstrates how to asynchronously query the Brave Search API for web search results, extract a summary key, and then use that key to fetch a detailed summary. It includes setup for logging, rate limiting, and API configuration, requiring a valid API key and Pro AI subscription for successful operation. ```python import asyncio import json import logging from urllib.parse import urljoin from aiohttp import ClientSession, ClientTimeout, TCPConnector from aiolimiter import AsyncLimiter # Configure logger logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) log = logging.getLogger(__name__) # Request rate and concurrency API_MAX_CONCURRENT_REQUESTS = 1 API_RPS = 1 API_RATE_LIMIT = AsyncLimiter(API_RPS, 1) API_TIMEOUT = 20 # Brave Search API Key API_KEY = "your_api_key" # Brave Search API host API_HOST = "https://api.search.brave.com" # Brave Search API subpaths API_PATH = { "web": urljoin(API_HOST, "res/v1/web/search"), "summarizer_search": urljoin(API_HOST, "res/v1/summarizer/search"), } # Create request headers for specific endpoints API_HEADERS = { "web": {"X-Subscription-Token": API_KEY, "Api-Version": "2023-10-11"}, "summarizer": {"X-Subscription-Token": API_KEY, "Api-Version": "2024-04-23"}, } # Create web search request params API_PARAMS_WEB = { "q": "what is the second highest mountain", "summary": 1, } async def get_summary(session: ClientSession) -> None: # Fetch web search results so we can get a summary key async with session.get( API_PATH["web"], params=API_PARAMS_WEB, headers=API_HEADERS["web"], ) as response: log.info("Querying url: [%s]", response.url) # print(await response.text()) data = await response.json() status = response.status if status != 200: log.error( "Failure getting web search results \n%s", json.dumps(data, indent=2), ) return # Get the summary key from web search results summary_key = data.get("summarizer", {}).get("key") if not summary_key: log.error("Failure: Getting summary key") return log.info("Summarizer Key: [%s]", summary_key) # Fetch summary all in one log.info("Requesting summarizer search in blocking mode") async with session.get( url=API_PATH["summarizer_search"], params={"key": summary_key, "entity_info": 1}, headers=API_HEADERS["summarizer"], ) as response: log.info("Querying url: [%s]", response.url) data = await response.json() status = response.status log.info(json.dumps(data, indent=2)) async def main(): async with API_RATE_LIMIT: async with ClientSession( connector=TCPConnector(limit=API_MAX_CONCURRENT_REQUESTS), timeout=ClientTimeout(API_TIMEOUT), ) as session: await get_summary(session=session) asyncio.run(main()) ``` -------------------------------- ### JavaScript: Fetch Images from API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This asynchronous function sends a GET request to the '/api/images' endpoint with a specified query parameter. It uses the Fetch API to retrieve image search results and returns the parsed JSON response. This function is a core dependency for loading image data. ```javascript async function fetchImages(query) { const params = new URLSearchParams({ q: query }); const response = await fetch(`/api/images?${params}`); return await response.json(); return data; } ``` -------------------------------- ### APIDOC: HowTo Schema Definition Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search/responses Defines the structure for aggregated information on a how-to guide. It includes fields for the how-to text, name, URL, and associated image URLs. ```APIDOC Class: HowTo Description: Aggregated information on a how to. Fields: - name: text type: string required: true description: The how to text. - name: name type: string required: false description: A name for the how to. - name: url type: string required: false description: A URL associated with the how to. - name: image type: list [ string ] required: false description: A list of image URLs associated with the how to. ``` -------------------------------- ### Execute Brave Search Summarizer Python Script Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/code-samples This command runs the `summarizer.py` script, which fetches summarized search results from the Brave Search Summarizer API. Before execution, ensure your API key is configured within the script and you have an active Pro AI subscription. ```bash python summarizer.py ``` -------------------------------- ### Suggest Search API Request Headers Reference Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/suggest/request-headers This section lists and describes all supported request headers for the Brave Suggest Search API. It includes information on their optionality, official names, and detailed explanations of their function, including specific examples for user agent strings and caching controls. ```APIDOC { "headers": [ { "header": "Accept", "required": false, "name": "Accept", "description": "The default supported media type is `application/json`" }, { "header": "Accept-Encoding", "required": false, "name": "Accept Encoding", "description": "The supported compression type is `gzip`." }, { "header": "Api-Version", "required": false, "name": "Web Search API Version", "description": "The Brave Web Search API version to use. This is denoted by the format `YYYY-MM-DD`. The latest version is used by default, and the previous ones can be found in the API Changelog." }, { "header": "Cache-Control", "required": false, "name": "Cache Control", "description": "Search will return cached web search results by default. To prevent caching, set the Cache-Control header to `no-cache`. This is currently done as best effort." }, { "header": "User-Agent", "required": false, "name": "User Agent", "description": "The user agent of the client sending the request. Search can utilize the user agent to provide a different experience depending on the client sending the request. The user agent should follow the commonly used browser agent strings on each platform. For more information on curating user agents, see RFC 9110. User agent string examples by platform:\n* Android: Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36\n* iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1\n* macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/\n* Windows: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" }, { "header": "X-Subscription-Token", "required": true, "name": "Authentication token", "description": "The secret token for the subscribed plan to authenticate the request. Can be obtained from API Keys." } ] } ``` -------------------------------- ### Example CURL Request for Brave Image Search API Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search This CURL command demonstrates how to make a basic image search request to the Brave API. It includes common query parameters like 'q' for query, 'safesearch', 'count', 'search_lang', 'country', and 'spellcheck', along with required 'Accept', 'Accept-Encoding', and 'X-Subscription-Token' headers for authentication and response format. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/images/search?q=munich&safesearch=strict&count=20&search_lang=en&country=us&spellcheck=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### News Search API Request Headers Reference Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/news-search/request-headers This reference lists all supported request headers for the Brave News Search API. Each entry specifies if the header is mandatory, its common name, and a detailed explanation of its use, including examples for User-Agent. ```APIDOC [ { "Header": "Accept", "Required": false, "Name": "Accept", "Description": "The default supported media type is `application/json`" }, { "Header": "Accept-Encoding", "Required": false, "Name": "Accept Encoding", "Description": "The supported compression type is `gzip`." }, { "Header": "Api-Version", "Required": false, "Name": "Web Search API Version", "Description": "The Brave Web Search API version to use. This is denoted by the format `YYYY-MM-DD`. The latest version is used by default, and the previous ones can be found in the API Changelog." }, { "Header": "Cache-Control", "Required": false, "Name": "Cache Control", "Description": "Search will return cached web search results by default. To prevent caching, set the Cache-Control header to `no-cache`. This is currently done as best effort." }, { "Header": "User-Agent", "Required": false, "Name": "User Agent", "Description": "The user agent of the client sending the request. Search can utilize the user agent to provide a different experience depending on the client sending the request. The user agent should follow the commonly used browser agent strings on each platform. For more information on curating user agents, see RFC 9110. User agent string examples by platform:\n* **Android**: Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36\n* **iOS**: Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1\n* **macOS**: Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/\n* **Windows**: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" }, { "Header": "X-Subscription-Token", "Required": true, "Name": "Authentication token", "Description": "The secret token for the subscribed plan to authenticate the request. Can be obtained from API Keys." } ] ``` -------------------------------- ### Example Location Results from Brave Web Search API (JSON) Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search This JSON snippet illustrates the structure of location results returned by the Brave Web Search API. Each location includes a temporary 'id' field, which is crucial for retrieving additional information about that specific location. This example shows multiple restaurant entries. ```JSON { "locations": { "results": [ { "id": "1520066f3f39496780c5931d9f7b26a6", "title": "Pangea Banquet Mediterranean Food" }, { "id": "d00b153c719a427ea515f9eacf4853a2", "title": "Park Mediterranean Grill" }, { "id": "4b943b378725432aa29f019def0f0154", "title": "The Halal Mediterranean Co." } ] } } ``` -------------------------------- ### CURL Example for Brave News Search API Request Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/news-search This CURL command demonstrates how to make a basic request to the Brave News Search API. It queries for 'munich' news, requests 10 results from the US, in English, with spellcheck enabled. Remember to replace '' with your actual subscription token obtained from the API Keys section. ```CURL curl -s --compressed "https://api.search.brave.com/res/v1/news/search?q=munich&count=10&country=us&search_lang=en&spellcheck=1" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Summarizer Search API Request Headers Reference Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/request-headers Detailed reference for all request headers supported by the Brave Summarizer Search API, including their purpose, whether they are required, and example values or formats where applicable. This section helps developers understand how to properly construct API requests. ```APIDOC [ { "header": "Accept", "required": false, "name": "Accept", "description": "The default supported media type is application/json" }, { "header": "Accept-Encoding", "required": false, "name": "Accept Encoding", "description": "The supported compression type is gzip." }, { "header": "Api-Version", "required": false, "name": "Web Search API Version", "description": "The Brave Web Search API version to use. This is denoted by the format YYYY-MM-DD. The latest version is used by default, and the previous ones can be found in the API Changelog." }, { "header": "Cache-Control", "required": false, "name": "Cache Control", "description": "Search will return cached web search results by default. To prevent caching, set the Cache-Control header to no-cache. This is currently done as best effort." }, { "header": "User-Agent", "required": false, "name": "User Agent", "description": "The user agent of the client sending the request. Search can utilize the user agent to provide a different experience depending on the client sending the request. The user agent should follow the commonly used browser agent strings on each platform. For more information on curating user agents, see RFC 9110. User agent string examples by platform:\n* **Android**: Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36\n* **iOS**: Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1\n* **macOS**: Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/\n* **Windows**: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" }, { "header": "X-Subscription-Token", "required": true, "name": "Authentication token", "description": "The secret token for the subscribed plan to authenticate the request. Can be obtained from API Keys." } ] ``` -------------------------------- ### Apply CSS Styles for Image Search Client UI Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This CSS block defines the visual presentation for an image search client. It structures the layout for image display, search input, and interactive buttons, ensuring a consistent and user-friendly interface. Dependencies include an HTML structure that references these IDs and classes. ```CSS #images { display: flex; flex-wrap: wrap; gap: 20px; max-width: 1000px; margin: 2rem auto; justify-content: center; } #search { display: flex; gap: 1rem; align-items: center; width: 600px; margin: auto; } #search input { width: 100%; padding: 10px 20px; border-radius: 12px; outline: none; border: solid 1px #d3d3d3; background: #f3f3f3; } button { cursor: pointer; padding: 10px 20px; border-radius: 12px; outline: none; border: solid 1px #d3d3d3; background: #f3f3f3; } button:hover { background: #111; color: #fff; } figure { border: solid 1px #d3d3d3; display: flex; flex-flow: column; padding: 5px 5px 10px 5px; max-width: 220px; gap: 1rem; border-radius: 12px; margin: 0; } img { max-width: 220px; max-height: 150px; object-fit: contain; border-radius: 12px; } figcaption { display: flex; text-align: center; font-family: sans-serif; font-size: 14px; flex-direction: column; gap: 5px; align-items: center; } figcaption .dimensions { font-size: 12px; color: blue; background: #e7e7fc; padding: 5px 10px; border-radius: 12px; width: fit-content; } ``` -------------------------------- ### Fetch AI-Generated Location Descriptions with Brave Search API (cURL) Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search Obtain AI-generated descriptions for locations using their unique IDs. This cURL example targets the /local/descriptions endpoint, passing multiple location IDs as parameters. Ensure your subscription token replaces for successful authentication. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/local/descriptions?ids=1520066f3f39496780c5931d9f7b26a6&ids=d00b153c719a427ea515f9eacf4853a2" \ -H "accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "x-subscription-token: " ``` -------------------------------- ### JavaScript: Orchestrate Image Loading and Display Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This main asynchronous function manages the entire image search workflow. It first fetches results, then attempts to load an initial set of 5 images in parallel. Subsequently, it loads additional images sequentially until at least 5 valid images are collected or all results are processed, finally calling `renderImages` to display them. ```javascript async function load(query) { // Load images from API const response = await fetchImages(query); const loadedImages = []; let i = 0; const count = 5; const initialPromises = []; // Load the first 5 images in parallel for (let i = 0; i < count; i++) { initialPromises.push(async () => { const result = response.results[i]; try { const image = await loadImage(result); loadedImages.push({ image, }); } catch (err) { // pass } }); } // Wait for the first 5 images to resolve await Promise.all(initialPromises); // Load images sequentially until there is 5 results, then show them. while (loadedImages.length < 5 || i >= response.results.length) { const result = response.results[i]; try { const image = await loadImage(result); loadedImages.push({ image, }); } catch (err) { // pass } i++; } renderImages(loadedImages); } ``` -------------------------------- ### HTML: Basic Image Search Page Structure Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/image-search/guides This HTML snippet defines the foundational structure of the image search page. It includes the document head with title and stylesheet, and the body containing a search input field, a button to trigger the JavaScript 'load' function, and a div container where images will be dynamically displayed. ```html Image Search
``` -------------------------------- ### Execute Brave Suggest API Request with cURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/suggest/get-started This cURL command demonstrates how to query the Brave Suggest API. It includes essential query parameters like 'q' for the search term, 'country', and 'count', along with necessary HTTP headers such as 'Accept', 'Accept-Encoding', and the 'X-Subscription-Token' for authentication. Replace with your actual API key obtained from the Brave dashboard. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/suggest/search?q=hello&country=US&count=5" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Perform Initial Web Search with CURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search/get-started Initiate a web search query to the Brave Search API using CURL. This request takes a "q" (query) parameter and requires an "X-Subscription-Token" for authentication. The API returns a JSON response that may include a list of locations, each with a temporary "id". ```curl curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=greek+restaurants+in+san+francisco" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Perform Initial Web Search with Brave Search API (cURL) Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search Initiate a web search request to the Brave Search API using cURL. This command queries for 'greek restaurants in san francisco' and expects a JSON response. Ensure to replace with your valid subscription token for authentication. ```Shell curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=greek+restaurants+in+san+francisco" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### APIDOC: Text Location Object Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/summarizer-search/responses Defines an index-based location within a text, specifying the 0-based start and end points of an important part of the text. ```APIDOC TextLocation: type: object description: "Index-based location in a text." properties: start: type: "integer" required: true description: "The 0-based index, where the important part of the text starts." end: type: "integer" required: true description: "The 0-based index, where the important part of the text ends." required: - start - end ``` -------------------------------- ### Brave News Search API Endpoint Reference Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/news-search/get-started This snippet provides the base URL for the Brave News Search API. This endpoint is used to send GET requests for news search results. ```APIDOC https://api.search.brave.com/res/v1/news/search ``` -------------------------------- ### Brave Local Search API Descriptions Endpoint Definition Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search This snippet defines the endpoint for the Brave Local Search API to get AI-generated descriptions for locations. This endpoint provides additional contextual information for local search results. ```APIDOC https://api.search.brave.com/res/v1/local/descriptions ``` -------------------------------- ### Spellcheck API Request Headers Reference Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/spellcheck/request-headers This section provides a detailed reference for all supported request headers for the Brave Spellcheck API. It outlines whether each header is required, its purpose, and specific examples where applicable, enabling developers to correctly form API requests. ```APIDOC [{"name":"Accept","required":false,"description":"The default supported media type is `application/json`."},{"name":"Accept-Encoding","required":false,"description":"The supported compression type is `gzip`."},{"name":"Api-Version","required":false,"description":"The Brave Web Search API version to use. This is denoted by the format `YYYY-MM-DD`. The latest version is used by default, and the previous ones can be found in the [API Changelog](./api-changelog)."},{"name":"Cache-Control","required":false,"description":"Search will return cached web search results by default. To prevent caching, set the Cache-Control header to `no-cache`. This is currently done as best effort."},{"name":"User-Agent","required":false,"description":"The user agent of the client sending the request. Search can utilize the user agent to provide a different experience depending on the client sending the request. The user agent should follow the commonly used browser agent strings on each platform. For more information on curating user agents, see [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#name-user-agent).","examples":{"Android":"Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36","iOS":"Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1","macOS":"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/","Windows":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"}},{"name":"X-Subscription-Token","required":true,"description":"The secret token for the subscribed plan to authenticate the request. Can be obtained from [API Keys](/app/keys)."}] ``` -------------------------------- ### Execute Brave Web Search API Request with cURL Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/index This cURL command demonstrates how to make a basic request to the Brave Web Search API. It queries for 'brave search' and expects a JSON response, requiring an API key for authentication via the 'X-Subscription-Token' header. Ensure you replace '' with your actual API key. ```bash curl -s --compressed "https://api.search.brave.com/res/v1/web/search?q=brave+search" \ -H "Accept: application/json" \ -H "Accept-Encoding: gzip" \ -H "X-Subscription-Token: " ``` -------------------------------- ### Brave Local Search API Descriptions Endpoint Source: https://api-dashboard.search.brave.com/app/documentation/news-search/get-started/web-search/get-started This snippet provides the endpoint for the Brave Local Search API to get AI-generated descriptions for locations. This is a specialized endpoint designed to offer enriched, descriptive content for specific geographical points. ```APIDOC https://api.search.brave.com/res/v1/local/descriptions ```