# `valstorm-plugin-linkedin` — LinkedIn Ads, Company & Personal Publishing Plugin (`NF-488` + `NF-440`) Unified multi-tenant **LinkedIn Campaign Manager (Ads)**, **Company Page Publishing**, **Personal Profile Publishing & Analytics**, and **PDF Carousel Upload** plugin for **Open Valstorm**, mounted at `platform.linkedin` (`VALSTORM_PLUGINS=linkedin` or `*`). Combines **NF-488** (*LinkedIn Ads & Publishing*) and **NF-440** (*LinkedIn Personal Posting & Analytics Integration*) into one first-class plugin: - **LinkedIn Ads (`platform.linkedin.ads`)**: Campaign Manager ad account discovery, campaign groups, campaigns, B2B firmographic audience size estimation (`/rest/audienceCounts`), Ad Library competitor intelligence (`/rest/adLibrary`), and campaign analytics (`/rest/adAnalytics`). - **Company & Personal Publishing (`platform.linkedin.publishing`)**: - Publish organic posts to either a **Company Page** (`urn:li:organization:...`) or a **Personal Profile** (`urn:li:person:...`). - Upload multi-page **PDF Carousels / Documents** (`/rest/documents?action=initializeUpload` + binary `PUT` + `/rest/posts`) directly from **Valstorm VFS** (`file_id`) or raw PDF bytes. - Query **Post-Level Engagement Analytics** (`impressions`, `uniqueImpressions`, `clicks`, `reactions`, `comments`, `reposts`), **Personal/Org Profile Metrics** (`followers`, `profile_views`, `search_appearances`), and **1st-Degree Network Connections**. --- ## 🤖 Agent-Assisted Quick Setup (Let the Valstorm Agent Set It Up) Because LinkedIn provides a built-in OAuth 2.0 Token Generator in the Developer Portal, you can grab a single access token (or Client ID + Secret) in 60 seconds and let your **Valstorm AI Agent** automatically discover your `person_urn` (`/v2/userinfo`), accessible Campaign Manager `ad_account_id` (`/rest/adAccounts`), and `organization_urn`, save the encrypted credentials, register $O(1)$ webhook routing, and verify live API connectivity. ### 1. Quick Human Prerequisite (60 Seconds in LinkedIn Developer Portal) 1. Open [linkedin.com/developers/apps](https://www.linkedin.com/developers/apps), select your app, and under **Products**, request access to **Advertising API**, **Share on LinkedIn**, **Sign In with LinkedIn using OpenID Connect**, and (optional) **LinkedIn Ad Library**. 2. Open the [LinkedIn OAuth 2.0 Token Generator](https://www.linkedin.com/developers/tools/oauth), select your app, check `openid`, `profile`, `email`, `w_member_social`, `r_ads`, `rw_ads`, `r_ads_reporting`, and (if using Company Page posting) `w_organization_social` / `r_organization_social`, and generate your **Access Token** (plus copy your **Client ID** and **Client Secret** from the **Auth** tab). ### 2. Copy-Paste Prompt for Your Valstorm Agent Drop this prompt into Valstorm Chat: > *"Set up `valstorm-plugin-linkedin` using my LinkedIn Access Token `` (Client ID: ``, Client Secret: ``, optional Company Organization ID: ``). Follow `plugins/valstorm-plugin-linkedin/README.md` to query `/v2/userinfo` and `/rest/adAccounts` to auto-discover my `person_urn` and `ad_account_id`, save the encrypted credentials via `platform.credentials.save('linkedin', ...)`, and run `linkedin_verify_connection`."* ### 3. Agent Execution Runbook (Instructions for the AI Agent) When a user asks you to set up `valstorm-plugin-linkedin`, execute this sequence autonomously: 1. **Save Initial Token & Auto-Discover Identity + Ad Accounts**: - Save the initial token via `await platform.credentials.save("linkedin", {"access_token": token, "client_id": ..., "client_secret": ...})`. - Call `await platform.linkedin.verify_connection()` (`linkedin_verify_connection`), which queries: - `GET https://api.linkedin.com/v2/userinfo` -> extracts `sub` and formats `person_urn = f"urn:li:person:{sub}"`. - `GET https://api.linkedin.com/rest/adAccounts?q=search` -> discovers accessible Campaign Manager `ad_account_id`s. 2. **Persist Complete Encrypted Credential & $O(1)$ Webhook Routing**: - Update `await platform.credentials.save("linkedin", {..., "person_urn": ..., "ad_account_id": ..., "organization_urn": ...})`. - Register `person_urn`, `organization_urn`, and `ad_account_id` in `external_account_routing` (`provider="linkedin"`). 3. **Confirm Setup**: - Report the authenticated member name/email, `person_urn`, discovered Ad Account(s), and `organization_urn` back to the user. --- ## 🛠️ End-to-End DIY Setup Guide (Manual) ### Step 1: Enable Products on Your LinkedIn Developer App 1. Go to [linkedin.com/developers/apps](https://www.linkedin.com/developers/apps) and open or create your app. 2. In the **Products** tab, request access to: - **Sign In with LinkedIn using OpenID Connect** (`openid`, `profile`, `email`) - **Share on LinkedIn** (`w_member_social` — for personal profile posting) - **Advertising API** (`r_ads`, `rw_ads`, `r_ads_reporting` — for Campaign Manager & B2B audience sizing) - **Community Management API** (`w_organization_social`, `r_organization_social`, `r_organization_admin` — for Company Page posting & analytics) - **LinkedIn Ad Library** (for competitor ad intelligence) ### Step 2: Configure Redirect URLs & Copy Client Credentials 1. Open the **Auth** tab of your LinkedIn Developer App. 2. Copy your **Client ID** and **Client Secret**. 3. Under **OAuth 2.0 settings -> Authorized redirect URLs for your app**, add: - Local: `http://localhost:8000/v1/linkedin/auth/callback` - Production: `https://api.yourdomain.com/v1/linkedin/auth/callback` ### Step 3: Save Encrypted Credentials in Open Valstorm ```python await platform.credentials.save( "linkedin", { "client_id": "YOUR_LINKEDIN_CLIENT_ID", "client_secret": "YOUR_LINKEDIN_CLIENT_SECRET", "access_token": "YOUR_LINKEDIN_OAUTH_ACCESS_TOKEN", "refresh_token": "YOUR_LINKEDIN_REFRESH_TOKEN", "ad_account_id": "509123456", "organization_urn": "urn:li:organization:98765432", "person_urn": "urn:li:person:AbCdEf123", "api_version": "202502", }, ) ``` --- ## Registered Object Prefixes & Marketplace App Bundle - **Object ID Prefix (`NF-490`)**: - `lnpo` -> `linkedin_post` - **Marketplace App Bundle (`NF-499`)**: - `app_linkedin` (**"LinkedIn Ads & Publishing"**) --- ## Python Usage Examples (`platform.linkedin`) ```python # 1. Verify connection, member identity (`person_urn`), and accessible Ad Accounts status = await platform.linkedin.verify_connection() # 2. Publish a Personal Profile or Company Page post (`NF-440` & `NF-488`) personal_post = await platform.linkedin.publishing.create_post( commentary="Excited to share our latest Open Valstorm plugin architecture benchmarks!", author_type="person", # or "organization" ) # 3. Upload & publish a multi-page PDF Carousel directly from Valstorm VFS (`file_id`) carousel_post = await platform.linkedin.publishing.upload_document_carousel( title="2026 Enterprise RevOps Benchmark Report", commentary="Swipe through our 8-slide breakdown of B2B response times 👇", file_id="file_987654321", author_type="organization", ) # 4. Fetch Post Engagement Analytics & Personal Profile Metrics (`NF-440`) post_stats = await platform.linkedin.publishing.get_post_analytics(personal_post["post_urn"]) profile_stats = await platform.linkedin.publishing.get_profile_metrics() # 5. Forecast B2B Audience Size & Create a PAUSED LinkedIn Ad Campaign (`NF-488`) reach = await platform.linkedin.ads.get_audience_size_estimate( targeting_criteria={ "include": { "and": [ {"or": {"urn:li:adTargetingFacet:locations": ["urn:li:geo:103644278"]}}, {"or": {"urn:li:adTargetingFacet:seniorities": ["urn:li:seniority:8", "urn:li:seniority:9"]}}, ] } } ) ``` --- ## REST API Endpoints (`/v1/linkedin/*`) | Method | Endpoint | Description | | :--- | :--- | :--- | | `GET` | `/v1/linkedin/setup-guide` | Step-by-step LinkedIn Developer & OAuth setup guide | | `GET` | `/v1/linkedin/auth/login` | Generate LinkedIn OAuth 2.0 authorization URL | | `GET` | `/v1/linkedin/auth/callback` | Exchange OAuth code for tokens & auto-discover `person_urn` | | `POST` | `/v1/linkedin/verify` | Verify token against `/v2/userinfo` and `/rest/adAccounts` | | `GET` | `/v1/linkedin/ads/accounts` | List accessible LinkedIn Campaign Manager ad accounts | | `GET` | `/v1/linkedin/ads/campaigns` | List LinkedIn ad campaigns | | `POST` | `/v1/linkedin/ads/campaigns` | Create a LinkedIn ad campaign (defaults to `PAUSED`) | | `POST` | `/v1/linkedin/ads/audience-estimate` | Estimate B2B targetable audience reach | | `GET` | `/v1/linkedin/ads/analytics` | Query campaign spend, impressions, clicks, and leads | | `GET` | `/v1/linkedin/ads/library` | Search competitor ads in the LinkedIn Ad Library | | `POST` | `/v1/linkedin/publishing/post` | Publish a Company Page or Personal Profile post (`lnpo_`) | | `POST` | `/v1/linkedin/publishing/carousel` | Upload a PDF Carousel from VFS (`file_id`) & publish post | | `GET` | `/v1/linkedin/publishing/analytics` | Fetch post-level engagement & profile performance metrics | | `GET` | `/v1/linkedin/network/connections` | List/track 1st-degree network connections & follower summary | | `POST` | `/v1/linkedin/webhooks` | Unauthenticated LinkedIn webhook receiver ($O(1)$ routing) | --- ## Running Tests ```bash cd apps/open-api .venv/bin/pytest tests/test_plugin_linkedin.py -v ```