{"library":"metaphor-node","title":"Metaphor Node.js SDK","description":"The `metaphor-node` package serves as the official JavaScript/TypeScript SDK for programmatically interacting with the Metaphor search API. It provides a robust client for accessing Metaphor's core functionalities, which include executing targeted searches, identifying content similar to a given URL, and retrieving the full textual contents of documents using their unique identifiers. Designed primarily for Node.js environments, the SDK is fully typed with TypeScript, enhancing developer experience and compile-time safety. The package is actively maintained within its `1.x.x` series, with recent updates such as `v1.5.13` reflecting ongoing development and feature enhancements. Internally, it utilizes `cross-fetch` to ensure broad compatibility for HTTP requests across different JavaScript runtimes, abstracting the complexities of underlying fetch implementations. This SDK is crucial for developers looking to integrate advanced, intelligent search and content discovery capabilities into their server-side applications with a straightforward and idiomatic API.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install metaphor-node"],"cli":null},"imports":["import Metaphor from 'metaphor-node'","import { type SearchOptions } from 'metaphor-node'","import { type SearchResponse } from 'metaphor-node'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Metaphor from 'metaphor-node';\n\n// Ensure METAPHOR_API_KEY is set in your environment variables.\n// Example: METAPHOR_API_KEY=\"your_api_key_here\"\nconst apiKey = process.env.METAPHOR_API_KEY ?? '';\n\nif (!apiKey) {\n  console.error('METAPHOR_API_KEY is not set. Please set it as an environment variable.');\n  process.exit(1);\n}\n\nconst metaphor = new Metaphor(apiKey);\n\nasync function runMetaphorSearch() {\n  try {\n    console.log('Performing search...');\n    const searchResponse = await metaphor.search('funny article about tech culture', {\n      numResults: 5,\n      includeDomains: ['nytimes.com', 'wsj.com'],\n      startPublishedDate: '2023-06-12'\n    });\n\n    console.log('Search Results:');\n    if (searchResponse.results && searchResponse.results.length > 0) {\n      for (const result of searchResponse.results) {\n        console.log(`- ${result.title} (${result.url})`);\n      }\n      \n      // Optionally, retrieve content for the first result\n      if (searchResponse.results[0]) {\n        console.log('\\nRetrieving content for the first result...');\n        const contentsResponse = await metaphor.getContents([searchResponse.results[0]]);\n        if (contentsResponse.contents && contentsResponse.contents.length > 0) {\n          console.log(`Content for '${contentsResponse.contents[0].title}':\\n${contentsResponse.contents[0].extract?.substring(0, 200)}...`);\n        }\n      }\n\n    } else {\n      console.log('No results found.');\n    }\n  } catch (error) {\n    console.error('An error occurred during Metaphor API call:', error);\n  }\n}\n\nrunMetaphorSearch();","lang":"typescript","description":"This quickstart demonstrates how to install, initialize, and perform a basic search using the Metaphor Node.js SDK, including fetching content for a result. It highlights the use of environment variables for the API key and includes basic error handling for asynchronous operations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}