feat: API Keys via Umgebungsvariablen konfigurierbar

- Neuer getApiKey() Helper: prüft zuerst ENV-Vars, dann DB
- Alle Job-Routes nutzen getApiKey() statt direktem DB-Lookup
- Credentials-Status berücksichtigt ENV-Vars (Sidebar-Haken)
- .env.local.example: Platzhalter für alle 4 API Keys

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Timo Uttenweiler
2026-03-20 13:58:41 +01:00
parent 39760b20a9
commit ea93d674a2
9 changed files with 54 additions and 49 deletions

View File

@@ -1,2 +1,8 @@
APP_ENCRYPTION_SECRET=your-32-character-secret-here!! APP_ENCRYPTION_SECRET=your-32-character-secret-here!!
DATABASE_URL=file:./leadflow.db DATABASE_URL=file:./leadflow.db
# API Keys — optional, überschreiben die Einstellungen in der UI
ANYMAILFINDER_API_KEY=
APIFY_API_KEY=
VAYNE_API_KEY=
GOOGLE_MAPS_API_KEY=

View File

@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { prisma } from "@/lib/db";
import { encrypt, decrypt } from "@/lib/utils/encryption"; import { encrypt, decrypt } from "@/lib/utils/encryption";
import { hasApiKeyFromEnv } from "@/lib/utils/apiKey";
const SERVICES = ["anymailfinder", "apify", "vayne", "airscale", "googlemaps"] as const; const SERVICES = ["anymailfinder", "apify", "vayne", "airscale", "googlemaps"] as const;
@@ -9,7 +10,7 @@ export async function GET() {
const creds = await prisma.apiCredential.findMany(); const creds = await prisma.apiCredential.findMany();
const result: Record<string, boolean> = {}; const result: Record<string, boolean> = {};
for (const svc of SERVICES) { for (const svc of SERVICES) {
result[svc] = creds.some(c => c.service === svc && c.value); result[svc] = hasApiKeyFromEnv(svc) || creds.some(c => c.service === svc && c.value);
} }
return NextResponse.json(result); return NextResponse.json(result);
} catch (err) { } catch (err) {

View File

@@ -1,17 +1,13 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { getApiKey } from "@/lib/utils/apiKey";
import { decrypt } from "@/lib/utils/encryption";
import axios from "axios"; import axios from "axios";
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
const service = req.nextUrl.searchParams.get("service"); const service = req.nextUrl.searchParams.get("service");
if (!service) return NextResponse.json({ ok: false, error: "Missing service" }, { status: 400 }); if (!service) return NextResponse.json({ ok: false, error: "Missing service" }, { status: 400 });
const cred = await prisma.apiCredential.findUnique({ where: { service } }); const key = await getApiKey(service);
if (!cred?.value) return NextResponse.json({ ok: false, error: "Not configured" }); if (!key) return NextResponse.json({ ok: false, error: "Not configured" });
const key = decrypt(cred.value);
if (!key) return NextResponse.json({ ok: false, error: "Empty key" });
try { try {
switch (service) { switch (service) {

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { prisma } from "@/lib/db";
import { decrypt } from "@/lib/utils/encryption"; import { getApiKey } from "@/lib/utils/apiKey";
import { cleanDomain } from "@/lib/utils/domains"; import { cleanDomain } from "@/lib/utils/domains";
import { bulkSearchDomains, type DecisionMakerCategory } from "@/lib/services/anymailfinder"; import { bulkSearchDomains, type DecisionMakerCategory } from "@/lib/services/anymailfinder";
@@ -16,11 +16,8 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "No companies provided" }, { status: 400 }); return NextResponse.json({ error: "No companies provided" }, { status: 400 });
} }
const cred = await prisma.apiCredential.findUnique({ where: { service: "anymailfinder" } }); const apiKey = await getApiKey("anymailfinder");
if (!cred?.value) { if (!apiKey) return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
}
const apiKey = decrypt(cred.value);
// Build domain → company map // Build domain → company map
const domainMap = new Map<string, string>(); const domainMap = new Map<string, string>();

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { prisma } from "@/lib/db";
import { decrypt } from "@/lib/utils/encryption"; import { getApiKey } from "@/lib/utils/apiKey";
import { import {
submitBulkPersonSearch, submitBulkPersonSearch,
getBulkSearchStatus, getBulkSearchStatus,
@@ -18,11 +18,8 @@ export async function POST(req: NextRequest) {
}; };
const { jobId, resultIds, categories } = body; const { jobId, resultIds, categories } = body;
const cred = await prisma.apiCredential.findUnique({ where: { service: "anymailfinder" } }); const apiKey = await getApiKey("anymailfinder");
if (!cred?.value) { if (!apiKey) return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
}
const apiKey = decrypt(cred.value);
const results = await prisma.leadResult.findMany({ const results = await prisma.leadResult.findMany({
where: { id: { in: resultIds }, jobId, domain: { not: null } }, where: { id: { in: resultIds }, jobId, domain: { not: null } },

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { prisma } from "@/lib/db";
import { decrypt } from "@/lib/utils/encryption"; import { getApiKey } from "@/lib/utils/apiKey";
import { searchPlacesMultiQuery } from "@/lib/services/googlemaps"; import { searchPlacesMultiQuery } from "@/lib/services/googlemaps";
import { bulkSearchDomains, type DecisionMakerCategory } from "@/lib/services/anymailfinder"; import { bulkSearchDomains, type DecisionMakerCategory } from "@/lib/services/anymailfinder";
@@ -20,17 +20,11 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "No search queries provided" }, { status: 400 }); return NextResponse.json({ error: "No search queries provided" }, { status: 400 });
} }
const mapsCredential = await prisma.apiCredential.findUnique({ where: { service: "googlemaps" } }); const mapsApiKey = await getApiKey("googlemaps");
if (!mapsCredential?.value) { if (!mapsApiKey) return NextResponse.json({ error: "Google Maps API key not configured" }, { status: 400 });
return NextResponse.json({ error: "Google Maps API key not configured" }, { status: 400 });
}
const mapsApiKey = decrypt(mapsCredential.value);
if (enrichEmails) { if (enrichEmails && !(await getApiKey("anymailfinder"))) {
const anymailCred = await prisma.apiCredential.findUnique({ where: { service: "anymailfinder" } }); return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
if (!anymailCred?.value) {
return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
}
} }
const job = await prisma.job.create({ const job = await prisma.job.create({
@@ -98,10 +92,8 @@ async function runMapsEnrich(
// 3. Optionally enrich with Anymailfinder // 3. Optionally enrich with Anymailfinder
if (params.enrichEmails && places.length > 0) { if (params.enrichEmails && places.length > 0) {
const anymailCred = await prisma.apiCredential.findUnique({ where: { service: "anymailfinder" } }); const anymailKey = await getApiKey("anymailfinder");
if (!anymailCred?.value) throw new Error("Anymailfinder key missing"); if (!anymailKey) throw new Error("Anymailfinder key missing");
const anymailKey = decrypt(anymailCred.value);
const domains = places.filter(p => p.domain).map(p => p.domain!); const domains = places.filter(p => p.domain).map(p => p.domain!);
// Map domain → placeId for updating results // Map domain → placeId for updating results

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { prisma } from "@/lib/db";
import { decrypt } from "@/lib/utils/encryption"; import { getApiKey } from "@/lib/utils/apiKey";
import { isSocialOrDirectory } from "@/lib/utils/domains"; import { isSocialOrDirectory } from "@/lib/utils/domains";
import { runGoogleSerpScraper, pollRunStatus, fetchDatasetItems } from "@/lib/services/apify"; import { runGoogleSerpScraper, pollRunStatus, fetchDatasetItems } from "@/lib/services/apify";
import { bulkSearchDomains, type DecisionMakerCategory } from "@/lib/services/anymailfinder"; import { bulkSearchDomains, type DecisionMakerCategory } from "@/lib/services/anymailfinder";
@@ -17,14 +17,10 @@ export async function POST(req: NextRequest) {
selectedDomains?: string[]; selectedDomains?: string[];
}; };
const apifyCred = await prisma.apiCredential.findUnique({ where: { service: "apify" } }); const [apifyToken, anymailKey] = await Promise.all([getApiKey("apify"), getApiKey("anymailfinder")]);
const anymailCred = await prisma.apiCredential.findUnique({ where: { service: "anymailfinder" } });
if (!apifyCred?.value) return NextResponse.json({ error: "Apify API token not configured" }, { status: 400 }); if (!apifyToken) return NextResponse.json({ error: "Apify API token not configured" }, { status: 400 });
if (!anymailCred?.value) return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 }); if (!anymailKey) return NextResponse.json({ error: "Anymailfinder API key not configured" }, { status: 400 });
const apifyToken = decrypt(apifyCred.value);
const anymailKey = decrypt(anymailCred.value);
const job = await prisma.job.create({ const job = await prisma.job.create({
data: { data: {

View File

@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db"; import { prisma } from "@/lib/db";
import { decrypt } from "@/lib/utils/encryption"; import { getApiKey } from "@/lib/utils/apiKey";
import { createOrder, getOrderStatus, triggerExport, downloadOrderCSV } from "@/lib/services/vayne"; import { createOrder, getOrderStatus, triggerExport, downloadOrderCSV } from "@/lib/services/vayne";
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
@@ -12,11 +12,8 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "Invalid Sales Navigator URL" }, { status: 400 }); return NextResponse.json({ error: "Invalid Sales Navigator URL" }, { status: 400 });
} }
const cred = await prisma.apiCredential.findUnique({ where: { service: "vayne" } }); const apiToken = await getApiKey("vayne");
if (!cred?.value) { if (!apiToken) return NextResponse.json({ error: "Vayne API token not configured" }, { status: 400 });
return NextResponse.json({ error: "Vayne API token not configured" }, { status: 400 });
}
const apiToken = decrypt(cred.value);
const job = await prisma.job.create({ const job = await prisma.job.create({
data: { data: {

23
lib/utils/apiKey.ts Normal file
View File

@@ -0,0 +1,23 @@
import { prisma } from "@/lib/db";
import { decrypt } from "./encryption";
const ENV_VARS: Record<string, string> = {
anymailfinder: "ANYMAILFINDER_API_KEY",
apify: "APIFY_API_KEY",
vayne: "VAYNE_API_KEY",
googlemaps: "GOOGLE_MAPS_API_KEY",
};
export async function getApiKey(service: string): Promise<string | null> {
const envVar = ENV_VARS[service];
if (envVar && process.env[envVar]) return process.env[envVar]!;
const cred = await prisma.apiCredential.findUnique({ where: { service } });
if (!cred?.value) return null;
return decrypt(cred.value);
}
export function hasApiKeyFromEnv(service: string): boolean {
const envVar = ENV_VARS[service];
return !!(envVar && process.env[envVar]);
}