cre-sync/lib/ghl/agency-client.ts
BusyBee3333 4e6467ffb0 Add CRESync CRM application with Setup page
- Build complete Next.js CRM for commercial real estate
- Add authentication with JWT sessions and role-based access
- Add GoHighLevel API integration for contacts, conversations, opportunities
- Add AI-powered Control Center with tool calling
- Add Setup page with onboarding checklist (/setup)
- Add sidebar navigation with Setup menu item
- Fix type errors in onboarding API, GHL services, and control center tools
- Add Prisma schema with SQLite for local development
- Add UI components with clay morphism design system

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:30:55 -05:00

72 lines
1.7 KiB
TypeScript

import { GHLError } from './errors';
export interface AgencyClientConfig {
agencyApiKey: string;
agencyId: string;
}
export class GHLAgencyClient {
private baseUrl = 'https://services.leadconnectorhq.com';
private agencyApiKey: string;
private agencyId: string;
constructor(config: AgencyClientConfig) {
this.agencyApiKey = config.agencyApiKey;
this.agencyId = config.agencyId;
}
async request<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
const url = `${this.baseUrl}${endpoint}`;
const response = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${this.agencyApiKey}`,
'Version': '2021-07-28',
'Content-Type': 'application/json',
...options.headers,
},
});
if (!response.ok) {
const errorBody = await response.text();
throw new GHLError(response.status, `GHL Agency API Error`, errorBody);
}
const text = await response.text();
if (!text) return {} as T;
return JSON.parse(text) as T;
}
// Create a new sub-account (location) for a user
async createLocation(data: {
name: string;
email: string;
phone?: string;
address?: string;
city?: string;
state?: string;
country?: string;
postalCode?: string;
timezone?: string;
}) {
return this.request('/locations/', {
method: 'POST',
body: JSON.stringify({
...data,
companyId: this.agencyId,
}),
});
}
// Get location details
async getLocation(locationId: string) {
return this.request(`/locations/${locationId}`);
}
// List all locations
async listLocations() {
return this.request(`/locations/search?companyId=${this.agencyId}`);
}
}