Registry / llm-agents / metaphor-node

metaphor-node

JSON →
library1.0.30jsnpmunverified

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.

npm install metaphor-node
INSTALL
IMPORT
SIG · METAPHOR-NODE
M
metaphor-node
llm-agentsjavascriptv1.0.30
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Metaphor
import Metaphor from 'metaphor-node'
const Metaphor = require('metaphor-node')
The library primarily uses ES Module syntax. For CommonJS environments, you might need to use `const Metaphor = require('metaphor-node').default;` or configure your build system for ESM interop.
SearchOptions
import { type SearchOptions } from 'metaphor-node'
Import types explicitly for TypeScript for clarity and to avoid runtime overhead.
SearchResponse
import { type SearchResponse } from 'metaphor-node'
Import types explicitly for TypeScript for clarity and to avoid runtime overhead.

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.

import Metaphor from 'metaphor-node'; // Ensure METAPHOR_API_KEY is set in your environment variables. // Example: METAPHOR_API_KEY="your_api_key_here" const apiKey = process.env.METAPHOR_API_KEY ?? ''; if (!apiKey) { console.error('METAPHOR_API_KEY is not set. Please set it as an environment variable.'); process.exit(1); } const metaphor = new Metaphor(apiKey); async function runMetaphorSearch() { try { console.log('Performing search...'); const searchResponse = await metaphor.search('funny article about tech culture', { numResults: 5, includeDomains: ['nytimes.com', 'wsj.com'], startPublishedDate: '2023-06-12' }); console.log('Search Results:'); if (searchResponse.results && searchResponse.results.length > 0) { for (const result of searchResponse.results) { console.log(`- ${result.title} (${result.url})`); } // Optionally, retrieve content for the first result if (searchResponse.results[0]) { console.log('\nRetrieving content for the first result...'); const contentsResponse = await metaphor.getContents([searchResponse.results[0]]); if (contentsResponse.contents && contentsResponse.contents.length > 0) { console.log(`Content for '${contentsResponse.contents[0].title}':\n${contentsResponse.contents[0].extract?.substring(0, 200)}...`); } } } else { console.log('No results found.'); } } catch (error) { console.error('An error occurred during Metaphor API call:', error); } } runMetaphorSearch();
Debug
Known issues
gotchaAlways provide your Metaphor API key via environment variables (`process.env.METAPHOR_API_KEY`) rather than hardcoding it directly in your application code. Hardcoding credentials is a significant security risk.
fix
Store your API key in a `.env` file or use a secure secret management system, loading it into `process.env` at runtime. E.g., `METAPHOR_API_KEY=your_key node your-app.js` or use `dotenv`.
affects: >=1.0.0
gotchaThe SDK is built with ES Modules (ESM) in mind. If you are working in an older CommonJS-only Node.js project, direct `require('metaphor-node')` might lead to unexpected behavior or an empty object. You may need to explicitly access the default export or configure your build process.
fix
For CommonJS, try `const Metaphor = require('metaphor-node').default;`. Consider migrating to ESM or using a transpiler like Babel if your project requires full ESM compatibility.
affects: >=1.0.0
gotchaAPI calls are asynchronous and can fail due to network issues, invalid API keys, incorrect parameters, or server-side errors. Implement robust `try...catch` blocks to handle potential errors gracefully and provide meaningful feedback.
fix
Wrap all Metaphor SDK calls in `try...catch` blocks when using `async/await`, or use `.catch()` with Promises, to handle and log errors appropriately.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'search')
The Metaphor client instance was not correctly initialized, often due to a missing or invalid API key.
fix
Ensure `new Metaphor(process.env.METAPHOR_API_KEY)` receives a valid API key string. Verify `process.env.METAPHOR_API_KEY` is correctly set and accessible in your environment.
ReferenceError: process is not defined
This error occurs when running Node.js-specific global variables (like `process`) in a non-Node.js environment, such as a browser, without proper polyfilling or bundling.
fix
Ensure your code is running in a Node.js environment or, if targeting the browser, bundle your application using a tool like Webpack or Rollup and provide appropriate polyfills for Node.js globals.
Error: Request failed with status code 401
The API key provided is invalid or unauthorized for the requested operation.
fix
Double-check your `METAPHOR_API_KEY` for correctness. Ensure it has the necessary permissions for the API calls you are making.
Error: Request failed with status code 429
You have exceeded the rate limits imposed by the Metaphor API.
fix
Implement exponential backoff or other rate-limiting strategies in your application to reduce the frequency of API requests. Review Metaphor's API documentation for specific rate limit details.
Upgrade
Version history
1.0.30latest on npm
Audit
Dependencies
cross-fetchrequiredUsed internally to provide a universal WHATWG Fetch API for Node.js environments, ensuring consistent HTTP request behavior.
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
metaphor-node — npm install metaphor-node · libregistry