@microfox/linkedin-share-sdk
A robust TypeScript SDK for sharing content on LinkedIn with built-in Zod validation.
Features
- 📘 Full TypeScript support with type definitions
- 🛡️ Runtime validation with Zod schemas
- 📱 Rich content sharing capabilities:
- Text posts
- Articles
- Images
- Videos
- Documents
- 🔒 Multiple visibility options (Public, Connections, Logged-in)
- ⚠️ Comprehensive error handling with descriptive messages
- 🎯 Simple, intuitive API
Installation
npm install @microfox/linkedin-share-sdk
yarn add @microfox/linkedin-share-sdk
pnpm add @microfox/linkedin-share-sdk
Usage
Basic Setup
import { LinkedinShareSdk } from '@microfox/linkedin-share-sdk';
const share = new LinkedinShareSdk('your_access_token');
Share Content
Simple Text Post
await share.post({
text: 'Hello LinkedIn!',
visibility: 'PUBLIC',
});
Share an Article
await share.post({
text: 'Check out this article!',
mediaCategory: 'ARTICLE',
media: [
{
url: 'https://example.com/article',
title: 'Amazing Article',
description: 'Must-read content',
},
],
});
Share an Image
await share.post({
text: 'Check out this image!',
mediaCategory: 'IMAGE',
media: [
{
url: 'https://example.com/image.jpg',
title: 'My Image',
thumbnailUrl: 'https://example.com/thumbnail.jpg',
},
],
});
Share with Custom Visibility
await share.post({
text: 'For my connections only',
visibility: 'CONNECTIONS',
});
Create a Draft Post
await share.post({
text: 'Draft post',
isDraft: true,
});
Validation and Error Handling
The SDK uses Zod for comprehensive validation at multiple levels:
- Input Validation: All post options are validated before processing
- Content Validation: The constructed share content is validated before sending to LinkedIn
- Response Validation: LinkedIn's response is validated to ensure correct format
Handling Validation Errors
import { z } from 'zod';
import { LinkedinShareSdk } from '@microfox/linkedin-share-sdk';
const share = new LinkedinShareSdk('your_access_token');
try {
await share.post({
text: 'Hello LinkedIn!',
mediaCategory: 'INVALID_CATEGORY',
});
} catch (error) {
if (error instanceof z.ZodError) {
console.error('Validation failed:', error.errors);
} else {
console.error('Share failed:', error.message);
}
}
Available Schemas
The SDK exports its Zod schemas if you need to validate data before calling the API:
import {
postOptionsSchema,
mediaContentSchema,
mediaCategorySchema,
visibilitySchema,
} from '@microfox/linkedin-share-sdk';
const validOptions = postOptionsSchema.parse({
text: 'Hello',
mediaCategory: 'IMAGE',
});
const validMedia = mediaContentSchema.parse({
url: 'https://example.com/image.jpg',
title: 'My Image',
});
Types
import type {
LinkedInPostOptions,
LinkedInShareContent,
LinkedInShareResponse,
LinkedInMediaContent,
LinkedInVisibility,
MediaCategory,
} from '@microfox/linkedin-share-sdk';
Media Categories
type MediaCategory = 'NONE' | 'ARTICLE' | 'IMAGE' | 'VIDEO' | 'DOCUMENT';
Visibility Options
type LinkedInVisibility = 'PUBLIC' | 'CONNECTIONS' | 'LOGGED_IN';
Post Options
interface LinkedInPostOptions {
text: string;
visibility?: LinkedInVisibility;
mediaCategory?: MediaCategory;
media?: LinkedInMediaContent[];
isDraft?: boolean;
}
Media Content
interface LinkedInMediaContent {
url?: string;
title?: string;
description?: string;
thumbnailUrl?: string;
}
License
MIT