Supabase Integration

Popular
Backend & Infrastructure

Store and query SERP data in Supabase PostgreSQL with real-time sync

Official Documentation

Features

PostgreSQL Storage

Store search results in a powerful relational database.

Real-time Subscriptions

Get notified when new search results are added.

Edge Functions

Run serverless functions to fetch and process SERP data.

Row Level Security

Secure your search data with fine-grained access control.

Setup

Step 1

Create Database Table

Set up a table to store search results.

CREATE TABLE search_results (
  id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
  query TEXT NOT NULL,
  engine TEXT NOT NULL,
  position INT,
  title TEXT,
  link TEXT,
  snippet TEXT,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now())
);

-- Add index for faster queries
CREATE INDEX idx_search_query ON search_results(query);
CREATE INDEX idx_search_created ON search_results(created_at DESC);
Step 2

Create Edge Function

Set up a serverless function to fetch search results.

// supabase/functions/search/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'

serve(async (req) => {
  const { query, engine = "google" } = await req.json()

  // Fetch from SERPII
  const response = await fetch(
    `https://api.serpii.com/v1/${engine}/light/search?q=${encodeURIComponent(query)}`,
    {
      headers: {
        'x-api-key': Deno.env.get('SERPII_API_KEY') || ''
      }
    }
  )
  const data = await response.json()

  // Store in Supabase
  const supabase = createClient(
    Deno.env.get('SUPABASE_URL') ?? '',
    Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
  )

  const results = data.organic_results.map(r => ({
    query,
    engine,
    position: r.position,
    title: r.title,
    link: r.link,
    snippet: r.snippet
  }))

  const { error } = await supabase
    .from('search_results')
    .insert(results)

  if (error) throw error

  return new Response(JSON.stringify(data), {
    headers: { 'Content-Type': 'application/json' }
  })
})

Code Examples

import { createClient } from '@supabase/supabase-js'

const supabase = createClient(
  process.env.SUPABASE_URL!,
  process.env.SUPABASE_ANON_KEY!
)

// Call edge function to search and store
async function searchAndStore(query: string) {
  const { data, error } = await supabase.functions.invoke('search', {
    body: { query, engine: 'google' }
  })

  if (error) throw error
  return data
}

// Query stored results
async function getRecentSearches() {
  const { data, error } = await supabase
    .from('search_results')
    .select('*')
    .order('created_at', { ascending: false })
    .limit(10)

  if (error) throw error
  return data
}

// Subscribe to new results
supabase
  .channel('search_results')
  .on('postgres_changes', {
    event: 'INSERT',
    schema: 'public',
    table: 'search_results'
  }, (payload) => {
    console.log('New search result:', payload.new)
  })
  .subscribe()

// Example usage
await searchAndStore("AI trends 2024")
const recent = await getRecentSearches()
console.log(recent)

Common Use Cases

  • Historical search data storage
  • Real-time search monitoring dashboards
  • SEO rank tracking over time
  • Competitive analysis database
  • Search result caching layer
  • Multi-user search applications