MCPcopy Index your code
hub / github.com/simstudioai/sim / createSubscription

Function createSubscription

apps/sim/lib/webhooks/providers/linq.ts:144–229  ·  view source on GitHub ↗
(ctx: SubscriptionContext)

Source from the content-addressed store, hash-verified

142 },
143
144 async createSubscription(ctx: SubscriptionContext): Promise<SubscriptionResult | undefined> {
145 const { webhook, requestId } = ctx
146 const providerConfig = getProviderConfig(webhook)
147 const apiKey = providerConfig.triggerApiKey as string | undefined
148 const triggerId = providerConfig.triggerId as string | undefined
149
150 if (!apiKey) {
151 logger.warn(`[${requestId}] Missing apiKey for Linq webhook creation.`, {
152 webhookId: webhook.id,
153 })
154 throw new Error(
155 'Linq API Key is required. Please provide your Linq API Key in the trigger configuration.'
156 )
157 }
158
159 const events =
160 triggerId === 'linq_webhook'
161 ? LINQ_ALL_WEBHOOK_EVENT_TYPES
162 : triggerId && LINQ_TRIGGER_TO_EVENT_TYPE[triggerId]
163 ? [LINQ_TRIGGER_TO_EVENT_TYPE[triggerId]]
164 : null
165
166 if (!events?.length) {
167 throw new Error(`Unknown or unsupported Linq trigger type: ${triggerId ?? '(missing)'}`)
168 }
169
170 const phoneNumbers = parsePhoneNumbers(providerConfig.triggerPhoneNumbers)
171 const requestBody: Record<string, unknown> = {
172 target_url: getNotificationUrl(webhook),
173 subscribed_events: events,
174 }
175 if (phoneNumbers.length > 0) {
176 requestBody.phone_numbers = phoneNumbers
177 }
178
179 logger.info(`[${requestId}] Creating Linq webhook subscription`, {
180 triggerId,
181 events,
182 webhookId: webhook.id,
183 })
184
185 const response = await fetch(`${LINQ_API_BASE}/webhook-subscriptions`, {
186 method: 'POST',
187 headers: linqHeaders(apiKey),
188 body: JSON.stringify(requestBody),
189 })
190
191 const responseBody = (await response.json().catch(() => ({}))) as Record<string, unknown>
192
193 if (!response.ok) {
194 const errorMessage =
195 ((responseBody.error as Record<string, unknown>)?.message as string) ||
196 (responseBody.message as string) ||
197 'Unknown Linq API error'
198 logger.error(
199 `[${requestId}] Failed to create Linq webhook subscription for webhook ${webhook.id}. Status: ${response.status}`,
200 { message: errorMessage }
201 )

Callers

nothing calls this directly

Calls 7

getProviderConfigFunction · 0.90
getNotificationUrlFunction · 0.90
linqHeadersFunction · 0.90
parsePhoneNumbersFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
warnMethod · 0.65

Tested by

no test coverage detected