Skip to main content

Functions API (Internal)

All Cloud Functions are deployed under your project region, e.g., https://<region>-<project>.cloudfunctions.net. Orchestrators and provider publish functions are invoked internally via Cloud Tasks. OAuth start/callback endpoints are called by the web app client.


Authentication

  • OAuth endpoints require Authorization: Bearer <Firebase ID token> header
  • Publish functions are invoked by Cloud Tasks (internal, no user auth needed)
  • Webhook endpoints validate platform-specific signatures (Stripe, Meta)

OAuth Endpoints (per platform)

Each platform follows the same pattern: *AuthStart initiates OAuth, *AuthCallback exchanges the code for tokens.

Facebook

EndpointMethodDescription
/fbAuthStartGETInitiates Facebook OAuth. Params: redirect=1, return_to=<url>. Requires ID token.
/fbAuthCallbackGETExchanges code for tokens, selects managed Page, stores credentials.

Instagram

EndpointMethodDescription
/igAuthStartGETInitiates Instagram OAuth (via Facebook Business). Requires ID token.
/igAuthCallbackGETExchanges code, maps IG Business Account via Page.

Threads

EndpointMethodDescription
/thAuthStartGETInitiates Threads OAuth (via Meta). Requires ID token.
/thAuthCallbackGETExchanges code for tokens.

X (Twitter)

EndpointMethodDescription
/xAuthStartGETOAuth 2.0 PKCE flow. Requires ID token.
/xAuthCallbackGETExchanges code for tokens.
/xAuth1StartGETOptional chained OAuth 1.0a (for v1.1 API media endpoints). Params: sid=<session>.
/xAuth1CallbackGETExchanges OAuth 1.0a verifier for tokens.

TikTok

EndpointMethodDescription
/ttAuthStartGETInitiates TikTok OAuth. Add ?desktop=1 for desktop auth flow. Requires ID token.
/ttAuthCallbackGETExchanges code for access/refresh tokens, fetches creator info.
/ttRefreshNowPOSTRefreshes a specific account's token on demand.

YouTube

EndpointMethodDescription
/ytAuthStartGETInitiates Google OAuth with offline access. Requires ID token.
/ytAuthCallbackGETExchanges code for access/refresh tokens.
/ytRefreshNowPOSTRefreshes a specific account's token on demand.

LinkedIn

EndpointMethodDescription
/liAuthStartGETInitiates LinkedIn OAuth 2.0. Requires ID token.
/liAuthCallbackGETExchanges code for tokens.

Snapchat

EndpointMethodDescription
/snapAuthStartGETInitiates Snapchat OAuth 2.0. Requires ID token.
/snapAuthCallbackGETExchanges code for access/refresh tokens.
/snapRefreshNowPOSTRefreshes a specific account's token on demand.

Publish Functions (Cloud Tasks)

These are invoked internally by the orchestrator via Cloud Tasks. Each receives { userId, postSubmissionId, request } in the body.

FunctionPlatformNotes
publishFacebookFacebookPages feed posting via Graph API
publishInstagramInstagramContainer creation + publish for images/video
publishThreadsThreadsText and image posting
publishXXTweet creation via v2 API
publishTiktokTikTokDirect publish for video/photo with status polling
publishYoutubeYouTubeVideo upload via Data API v3
publishLinkedinLinkedInUGC Posts API
snapPostSnapchatSnap post creation

Orchestrators

FunctionDescription
publishOrchestratorFan-out orchestration via Cloud Tasks. Body: { userId, postSubmissionId, request }
repostOrchestratorAutomated repost flow. Body: { userId, postId }
clipperOrchestratorMedia transformation (image-to-video conversion)

User-Facing Functions

FunctionMethodDescription
publishPostPOSTRequires Firebase ID token. Body: { post, scheduledTime? }. Creates submission and orchestrates.
cancelScheduledPostPOSTCancels a previously scheduled post submission.
transcribeMediaPOSTAI transcription via OpenAI Whisper.
reportAiUsagePOSTReports AI feature usage for analytics.
convertImageToVideoPOSTConverts static image to video format.

Media & Webhooks

FunctionMethodDescription
mediaGET/HEADMedia proxy: /media/{id}[.{ext}] — streams from Firebase Storage
stripeWebhookPOSTStripe events: subscription lifecycle, invoices
metaWebhookGET/POSTMeta verification (GET) and event receiver (POST)
api*Express app serving all /v1/* Public API routes

Scheduled Functions

FunctionSchedulePurpose
ttRefreshSweepPeriodicRefresh expiring TikTok tokens
ytRefreshSweepPeriodicRefresh expiring YouTube tokens
snapRefreshSweepPeriodicRefresh expiring Snapchat tokens
scheduledTikTokPollPeriodicPoll TikTok for new organic posts
scheduledInstagramPollPeriodicPoll Instagram for new organic posts
scheduledXPollPeriodicPoll X for new posts
scheduledYouTubePollPeriodicPoll YouTube for new videos
scheduledTikTokMetricsPeriodicCollect TikTok post metrics
scheduledFacebookMetricsPeriodicCollect Facebook post metrics
scheduledThreadsMetricsPeriodicCollect Threads post metrics
scheduledRateLimitCleanupPeriodicRemove expired rate limit entries
scheduledDailyMetricsSnapshotDailySnapshot aggregate metrics
integrationsHealthSweepPeriodicCheck all integration connection health

Firestore Triggers

FunctionTriggerPurpose
onOrganicPostCreatedorganicPosts/* writeInitiates repost workflows
onFacebookAccountCreatedIntegration account createPost-connection setup
onInstagramAccountCreatedIntegration account createPost-connection setup
onTiktokAccountCreatedIntegration account createPost-connection setup
onXAccountCreatedIntegration account createPost-connection setup
onLinkedinAccountCreatedIntegration account createPost-connection setup
onIntegrationAccountAvatarChangeAvatar field updateHost avatar on Firebase Storage
onAccountDeletedAccount deletionClean up related workflows
onWorkflowWrittenWorkflow doc writeSync source account metadata
onWorkflowDeletedWorkflow doc deleteClean up source account sync
onTemplateWrittenTemplate doc writeGenerate template previews
onLibraryAssetUploadedLibrary asset uploadGenerate thumbnails

Error Handling

All API responses follow a structured error format:

{
"error": {
"code": "validation_error",
"message": "Request validation failed",
"timestamp": "2025-08-29T00:02:14.654Z",
"requestId": "abb6930d-2b57-4d44-9d63-19d5a9c8d077"
}
}

Common Error Codes

CodeStatusDescription
unauthorized401Missing or invalid authentication
forbidden403Insufficient permissions or inactive subscription
not_found404Resource not found
validation_error400Request validation failed (Zod schema errors)
rate_limit_exceeded429Too many requests
internal_error500Server error

Request Tracking

Include X-Request-ID header for request correlation across logs:

curl -H "X-Request-ID: debug-123" \
-H "soku-api-key: your_key" \
https://api.example.com/v1/posts

Errors are logged to the errorLogs Firestore collection and to stdout for Cloud Logging. Set NODE_ENV=development for detailed error responses with stack traces.


Frontend Integration

The web app interacts with these functions through:

  • useConnectIntegration hook — calls startAuth(platform, idToken, returnTo, isLocal) and navigates to the returned authUrl
  • Integration context — reads connected accounts from users/{uid}/integrations/*
  • Publish hooks — submit posts via the Public API (/v1/posts), which internally orchestrates to the publish functions above