Linkedin marketing

From TerminalSkills/skills · View interactive version

Linkedin marketing is an AI agent skill from the GitHub repository TerminalSkills/skills. Create, optimize, and automate LinkedIn content for personal branding, lead generation, and B2B marketing. Use when someone asks to "write a LinkedIn post", "grow on LinkedIn", "LinkedIn content strategy", "LinkedIn API integration", "automate LinkedIn posting", "LinkedIn carousel", "LinkedIn analyt

How to use this skill

Skill file

LinkedIn Marketing

Overview

This skill helps AI agents write LinkedIn posts and publish them through the LinkedIn Posts API. It covers post structure, document (carousel) and image posts, company page posting, OAuth setup, and the access limits that decide what a script can read back.

The API facts below were checked against the LinkedIn Marketing API reference on Microsoft Learn. Writing advice is a heuristic: LinkedIn does not publish its ranking formula or best posting times, so the numbers in the growth section are starting points, not rules.

Instructions

Post Rules

Content Formats

Text Posts
[Hook: first two lines show before "...see more"]

[Short paragraphs, one to three sentences each]

[Body: story, insight, or framework]

[CTA: an open question people can answer]

[Hashtags: a few relevant ones, or none]
Document Posts (carousels)

LinkedIn's "carousel" is a document post: upload a PDF, PPT, PPTX, DOC or DOCX file. Limits from the Documents API: 100 MB and 300 pages per file. Each page is one slide.

Polls
Articles
Video

LinkedIn Posts API Integration

Every call needs two headers: Linkedin-Version: YYYYMM and X-Restli-Protocol-Version: 2.0.0. Use a version from the current Posts API docs. The version 202511 is scheduled to be sunset on 16 November 2026, so do not start new code on it.

Authentication (3-legged OAuth 2.0)

Authorize at https://www.linkedin.com/oauth/v2/authorization, then exchange the code at https://www.linkedin.com/oauth/v2/accessToken.

const authUrl = new URL('https://www.linkedin.com/oauth/v2/authorization');
authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('client_id', process.env.LINKEDIN_CLIENT_ID!);
authUrl.searchParams.set('redirect_uri', process.env.LINKEDIN_REDIRECT_URI!);
authUrl.searchParams.set('state', crypto.randomUUID()); // check it on return
authUrl.searchParams.set('scope', 'openid profile w_member_social');
// Send the member's browser to authUrl.toString()

// After the redirect, verify `state`, then exchange the code (valid 30 minutes):
const tokenRes = await fetch('https://www.linkedin.com/oauth/v2/accessToken', {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: new URLSearchParams({
    grant_type: 'authorization_code',
    code: authorizationCode,
    client_id: process.env.LINKEDIN_CLIENT_ID!,
    client_secret: process.env.LINKEDIN_CLIENT_SECRET!,
    redirect_uri: process.env.LINKEDIN_REDIRECT_URI!,
  }),
});
const { access_token, expires_in } = await tokenRes.json();
// expires_in is 5184000 seconds (60 days) on current tokens.

LinkedIn does not issue long-lived access tokens to most apps. Plan to send the member through the consent flow again when the token expires. Programmatic refresh tokens are limited to approved partners.

Text Post
const headers = {
  Authorization: `Bearer ${access_token}`,
  'Content-Type': 'application/json',
  'X-Restli-Protocol-Version': '2.0.0',
  'Linkedin-Version': process.env.LINKEDIN_API_VERSION!, // a current YYYYMM value
};

const postRes = await fetch('https://api.linkedin.com/rest/posts', {
  method: 'POST',
  headers,
  body: JSON.stringify({
    author: `urn:li:person:${personId}`,
    commentary: 'We cut our export job from 40 minutes to 90 seconds.\n\nThe fix was not a bigger server.',
    visibility: 'PUBLIC',
    distribution: { feedDistribution: 'MAIN_FEED', targetEntities: [], thirdPartyDistributionChannels: [] },
    lifecycleState: 'PUBLISHED',
    isReshareDisabledByAuthor: false,
  }),
});
// 201 Created; the post URN is in the x-restli-id response header.
const postUrn = postRes.headers.get('x-restli-id');

personId is the member's ID. Get it from the OpenID Connect userinfo response (GET https://api.linkedin.com/v2/userinfo with the bearer token; the sub field; needs the openid and profile scopes and the "Sign In with LinkedIn using OpenID Connect" product on the app), then build urn:li:person:{sub}.

Image Post
// 1. Register the upload
const init = await fetch('https://api.linkedin.com/rest/images?action=initializeUpload', {
  method: 'POST',
  headers,
  body: JSON.stringify({ initializeUploadRequest: { owner: `urn:li:person:${personId}` } }),
});
const { value: { uploadUrl, image } } = await init.json();

// 2. Upload the bytes to the returned uploadUrl
await fetch(uploadUrl, {
  method: 'PUT',
  headers: { Authorization: `Bearer ${access_token}` },
  body: imageBuffer, // Buffer with the PNG or JPG bytes
});

// 3. Create the post that references the image URN
await fetch('https://api.linkedin.com/rest/posts', {
  method: 'POST',
  headers,
  body: JSON.stringify({
    author: `urn:li:person:${personId}`,
    commentary: 'Latency chart from this week.',
    visibility: 'PUBLIC',
    distribution: { feedDistribution: 'MAIN_FEED', targetEntities: [], thirdPartyDistributionChannels: [] },
    content: { media: { altText: 'Line chart of p95 latency falling from 420 ms to 90 ms', id: image } },
    lifecycleState: 'PUBLISHED',
    isReshareDisabledByAuthor: false,
  }),
});

Images must be JPG, GIF or PNG and under 36,152,320 pixels. The upload step for a file that is not an image is different, so use the Documents API for PDFs.

Document (Carousel) Post
// 1. Register the document upload
const docInit = await fetch('https://api.linkedin.com/rest/documents?action=initializeUpload', {
  method: 'POST',
  headers,
  body: JSON.stringify({ initializeUploadRequest: { owner: `urn:li:person:${personId}` } }),
});
const { value: { uploadUrl: docUploadUrl, document } } = await docInit.json();

// 2. Upload the PDF to the returned uploadUrl
await fetch(docUploadUrl, {
  method: 'PUT',
  headers: { Authorization: `Bearer ${access_token}` },
  body: pdfBuffer, // under 100 MB and 300 pages
});

// 3. Post it. Use the document URN, not the image URN.
await fetch('https://api.linkedin.com/rest/posts', {
  method: 'POST',
  headers,
  body: JSON.stringify({
    author: `urn:li:person:${personId}`,
    commentary: '7 slides on how we cut CI time by 60%.',
    visibility: 'PUBLIC',
    distribution: { feedDistribution: 'MAIN_FEED', targetEntities: [], thirdPartyDistributionChannels: [] },
    content: { media: { title: 'CI speedup playbook', id: document } },
    lifecycleState: 'PUBLISHED',
    isReshareDisabledByAuthor: false,
  }),
});

Wait for the document to finish processing before creating the post. Poll GET /rest/documents/{urn} until status is AVAILABLE. A PROCESSING_FAILED status means the file must be replaced.

Company Page Posting

Post as an organization with author: "urn:li:organization:{id}". The authenticated member must hold one of these page roles: ADMINISTRATOR, DIRECT_SPONSORED_CONTENT_POSTER or CONTENT_ADMIN. The required scope is w_organization_social. Reading organization posts needs r_organization_social and the same roles.

Reading Posts and Analytics

Growth Strategy (heuristic)

These are working habits, not published LinkedIn rules. LinkedIn does not publish weights for comments, dwell time or links.

Rhythm: a few posts a week is a common starting point. Consistency usually matters more than one post's result.

Timing: check your own analytics by weekday and hour. Do not rely on a fixed "best time" list, since none is published.

Content mix:

Comment strategy:

Examples

Example 1: Write a LinkedIn post announcing a product launch

User prompt: "Write a LinkedIn post for me. I'm the CTO of Meridian Analytics and we just launched a real-time data pipeline product called StreamFlow that processes 2 million events per second. Target audience is engineering leaders at mid-size SaaS companies."

The agent drafts a text post in the first person. The first two lines are the hook: "We spent 14 months building something we could not find anywhere else. Today we are launching StreamFlow, a real-time pipeline that handles 2 million events per second." The body describes the problem (batch jobs left dashboards hours behind), what StreamFlow changes, and one result from beta customers. It ends with an open question: "What is the slowest step in your data pipeline today?" The product URL goes in the first comment, not the body. The agent also checks the draft against the commentary length limit in the Posts API schema before publishing.

Example 2: Automate a weekly post to a LinkedIn company page

User prompt: "Build a TypeScript function that posts a weekly engineering blog summary to our LinkedIn company page (Meridian Analytics, org ID 91847362) every Monday at 9 AM EST."

The agent writes a module that reads a short access token from LINKEDIN_ACCESS_TOKEN and calls POST https://api.linkedin.com/rest/posts with author: "urn:li:organization:91847362", the summary as commentary, visibility: "PUBLIC", and lifecycleState: "PUBLISHED". Headers include Linkedin-Version with a current YYYYMM value and X-Restli-Protocol-Version: 2.0.0. It runs on node-cron with 0 9 * * 1 and the time zone set to America/New_York. Before the first run, it checks that the token has w_organization_social and that the member is an ADMINISTRATOR, DIRECT_SPONSORED_CONTENT_POSTER or CONTENT_ADMIN of that page. It logs the returned x-restli-id on success and the error body on failure. It also records when the 60-day token will expire and sends an alert before then.

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.