Skip to content

An AI-powered search engine with a generative UI

feat: impl simple firecrawl sdk

Yazalde Filimoneb187402a2890
diff --git a/lib/firecrawl/client.ts b/lib/firecrawl/client.ts
new file mode 100644
index 00000000..f4e71c42
--- /dev/null
+++ b/lib/firecrawl/client.ts
@@ -0,0 +1,98 @@
+import {
+ FirecrawlImageSearchOptions,
+ FirecrawlImageSearchResponse,
+ FirecrawlSearchOptions,
+ FirecrawlSearchResponse
+} from './types'
+
+export class FirecrawlClient {
+ private readonly apiKey: string
+ private readonly baseUrl = 'https://api.firecrawl.dev/v2'
+
+ constructor(apiKey: string) {
+ this.apiKey = apiKey
+ }
+
+ async search(
+ options: FirecrawlSearchOptions
+ ): Promise<FirecrawlSearchResponse> {
+ const body = JSON.stringify({
+ query: options.query,
+ sources: options.sources || ['web'],
+ limit: options.limit || 10,
+ location: options.location,
+ tbs: options.tbs,
+ scrapeOptions: {
+ formats: ['markdown'],
+ proxy: 'auto',
+ blockAds: true
+ }
+ })
+ const response = await fetch(`${this.baseUrl}/search`, {
+ method: 'POST',
+ headers: this.getHeaders(),
+ body
+ })
+
+ return this.handleResponse<FirecrawlSearchResponse>(response)
+ }
+
+ async searchImages(
+ options: FirecrawlImageSearchOptions
+ ): Promise<FirecrawlImageSearchResponse> {
+ const body = JSON.stringify({
+ query: options.query,
+ sources: ['images'],
+ limit: options.limit || 8
+ })
+
+ const response = await fetch(`${this.baseUrl}/search`, {
+ method: 'POST',
+ headers: this.getHeaders(),
+ body
+ })
+
+ return this.handleResponse<FirecrawlImageSearchResponse>(response)
+ }
+
+ async getImagesForQuery(
+ query: string,
+ maxResults: number = 8
+ ): Promise<{ url: string; description: string }[]> {
+ try {
+ const searchResponse = await this.searchImages({
+ query,
+ limit: maxResults
+ })
+
+ const data = searchResponse.data
+
+ if (!searchResponse.success || !data.images) return []
+
+ return data.images.map(image => ({
+ url: image.imageUrl,
+ description: image.title ?? ''
+ }))
+ } catch (error) {
+ console.error('Firecrawl image search error:', error)
+ return []
+ }
+ }
+
+ private getHeaders(): Record<string, string> {
+ return {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${this.apiKey}`
+ }
+ }
+
+ private async handleResponse<T>(response: Response): Promise<T> {
+ if (!response.ok) {
+ const errorText = await response.text()
+ throw new Error(
+ `Firecrawl status: ${response.status}, reason error: ${errorText}`
+ )
+ }
+ return response.json()
+ }
+}
diff --git a/lib/firecrawl/index.ts b/lib/firecrawl/index.ts
new file mode 100644
index 00000000..edfed4a6
--- /dev/null
+++ b/lib/firecrawl/index.ts
@@ -0,0 +1,2 @@
+export * from './client'
+export * from './types'
diff --git a/lib/firecrawl/types.ts b/lib/firecrawl/types.ts
new file mode 100644
index 00000000..8a9ccd74
--- /dev/null
+++ b/lib/firecrawl/types.ts
@@ -0,0 +1,59 @@
+export type FirecrawlSource = 'web' | 'news' | 'images'
+
+export type FirecrawlSearchOptions = {
+ query: string
+ sources?: FirecrawlSource[]
+ limit?: number
+ location?: string
+ tbs?: string
+}
+
+export type FirecrawlImageSearchOptions = {
+ query: string
+ sources?: ['images']
+ limit?: number
+}
+
+export type FirecrawlImageResult = {
+ title: string
+ imageUrl: string
+ imageWidth: number
+ imageHeight: number
+ url: string
+ position: number
+}
+
+export type FirecrawlWebResult = {
+ url: string
+ title: string
+ description: string
+ markdown: string
+ position: number
+}
+
+export type FirecrawlNewsResult = {
+ title: string
+ url: string
+ snippet: string
+ date: string
+ position: number
+}
+
+type FirecrawlSearchResponseData = {
+ web?: FirecrawlWebResult[]
+ news?: FirecrawlNewsResult[]
+ images?: FirecrawlImageResult[]
+}
+export type FirecrawlSearchResponse = {
+ success: boolean
+ data: FirecrawlSearchResponseData
+}
+
+type FirecrawlImageSearchResponseData = {
+ images?: FirecrawlImageResult[]
+}
+
+export type FirecrawlImageSearchResponse = {
+ success: boolean
+ data: FirecrawlImageSearchResponseData
+}