Skip to content

Commit 103107d

Browse files
committed
Document getting getMdBySearchId straight after running getMd
1 parent 24bee23 commit 103107d

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,21 +404,20 @@ Get a Markdown response given a search ID.
404404
#### Examples
405405

406406
```javascript
407-
// Submit as JSON so the search ID is available in the structured response.
408-
const response = await getJson({
407+
const markdown = await getMd({
409408
engine: "google",
410409
api_key: API_KEY,
411-
async: true,
412410
q: "coffee",
413411
});
414-
const { id } = response.search_metadata;
415-
await delay(1000); // wait for the request to be processed.
412+
const idMatch = markdown.match(/^ id:\s*(.+)$/m);
413+
if (!idMatch) throw new Error("Search ID missing from Markdown frontmatter");
414+
const searchId = idMatch[1].trim();
416415

417416
// async/await
418-
const markdown = await getMdBySearchId(id, { api_key: API_KEY });
417+
const archivedMarkdown = await getMdBySearchId(searchId, { api_key: API_KEY });
419418

420419
// callback
421-
getMdBySearchId(id, { api_key: API_KEY }, console.log);
420+
getMdBySearchId(searchId, { api_key: API_KEY }, console.log);
422421
```
423422

424423
### getAccount

‎src/serpapi.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,16 @@ export async function getHtmlBySearchId(
350350
* @param {number=} [parameters.timeout] Timeout in milliseconds.
351351
* @param {fn=} callback Optional callback.
352352
* @example
353-
* // Submit as JSON so the search ID is available in the structured response.
354-
* const response = await getJson({ engine: "google", api_key: API_KEY, async: true, q: "coffee" });
355-
* const { id } = response.search_metadata;
356-
* await delay(1000); // wait for the request to be processed.
353+
* const markdown = await getMd({ engine: "google", api_key: API_KEY, q: "coffee" });
354+
* const idMatch = markdown.match(/^ id:\s*(.+)$/m);
355+
* if (!idMatch) throw new Error("Search ID missing from Markdown frontmatter");
356+
* const searchId = idMatch[1].trim();
357357
*
358358
* // async/await
359-
* const markdown = await getMdBySearchId(id, { api_key: API_KEY });
359+
* const archivedMarkdown = await getMdBySearchId(searchId, { api_key: API_KEY });
360360
*
361361
* // callback
362-
* getMdBySearchId(id, { api_key: API_KEY }, console.log);
362+
* getMdBySearchId(searchId, { api_key: API_KEY }, console.log);
363363
*/
364364
export async function getMdBySearchId(
365365
searchId: string,

0 commit comments

Comments
 (0)