{
  "slug": "protect-supabase-service-role-key-nextjs",
  "title": "How to Protect Supabase Service Role Key in Next.js & Client Apps",
  "metaTitle": "Protect Supabase Service Role Key in Next.js - RealComposer",
  "metaDescription": "Protect your Supabase service role key in Next.js. Store keys in RealComposer Secret Vault and expose locked-down edge endpoints with output filtering.",
  "headline": "Secure Supabase Admin Proxy at the Edge",
  "category": "security",
  "categoryLabel": "Security & Secret Vault",
  "urgency": 10,
  "clientPlatform": "Next.js / React",
  "upstreamServices": [
    "Supabase"
  ],
  "searchIntentQuery": "how to hide supabase service role key in nextjs",
  "directAnswerSummary": "The Supabase service_role key bypasses all Row Level Security (RLS) rules. If leaked, an attacker has unrestricted admin access to your database. RealComposer keeps your service role key in the Secret Vault and exposes locked-down endpoints with strict output filtering.",
  "problem": {
    "title": "Service Role Keys Bypass All Database Security",
    "explanation": "Accidentally importing a service_role key into client components gives users full database permissions.",
    "risks": [
      "Complete database exfiltration",
      "Bypassing Row Level Security (RLS)",
      "Malicious record updates"
    ]
  },
  "solutionSteps": [
    {
      "stepNumber": 1,
      "title": "Keep Service Role Key in Vault",
      "description": "Store SUPABASE_SERVICE_ROLE_KEY in RealComposer."
    },
    {
      "stepNumber": 2,
      "title": "Project Only Safe Fields",
      "description": "Strip sensitive columns like password_hash or internal audit fields."
    }
  ],
  "pipelineConfig": {
    "name": "Supabase Admin Proxy",
    "method": "GET",
    "cacheTtlSeconds": 30,
    "steps": [
      {
        "stepKey": "userQuery",
        "upstreamUrlTemplate": "https://xyz.supabase.co/rest/v1/users?id=eq.{{query.userId}}",
        "httpMethod": "GET",
        "headersTemplate": {
          "apikey": "{{secrets.SUPABASE_SERVICE_ROLE_KEY}}",
          "Authorization": "Bearer {{secrets.SUPABASE_SERVICE_ROLE_KEY}}"
        },
        "timeoutMs": 3000
      }
    ],
    "outputMapping": {
      "version": 1,
      "fields": [
        {
          "targetPath": "userId",
          "sourceStep": "userQuery",
          "sourcePath": "0.id"
        },
        {
          "targetPath": "email",
          "sourceStep": "userQuery",
          "sourcePath": "0.email"
        },
        {
          "targetPath": "role",
          "sourceStep": "userQuery",
          "sourcePath": "0.role"
        }
      ]
    }
  },
  "sampleBloatedInput": [
    {
      "id": "u_1",
      "email": "user@example.com",
      "role": "admin",
      "password_hash": "$2b$12$...",
      "ssn_last4": "1234"
    }
  ],
  "sampleSlimmedOutput": {
    "userId": "u_1",
    "email": "user@example.com",
    "role": "admin"
  },
  "curlSnippet": "curl -X GET \"https://realcomposer.com/v1/acme/user-admin-view?userId=u_1\"",
  "faqs": [
    {
      "question": "Why use RealComposer instead of Supabase Edge Functions?",
      "answer": "RealComposer requires zero code or Deno deployments and provides visual field trimming."
    }
  ],
  "relatedRecipeSlugs": [
    "merge-supabase-and-stripe-into-single-endpoint",
    "hide-openai-api-key-react"
  ],
  "links": {
    "page": "https://realcomposer.com/recipes/protect-supabase-service-role-key-nextjs",
    "markdown": "https://realcomposer.com/recipes/protect-supabase-service-role-key-nextjs.md",
    "json": "https://realcomposer.com/recipes/protect-supabase-service-role-key-nextjs.json",
    "install": "https://realcomposer.com/dashboard/pipelines/new?recipe=protect-supabase-service-role-key-nextjs&via=agent"
  }
}