The LinkedIn API allows developers to connect with LinkedIn’s network of over 740 million professionals. With the LinkedIn API, you can build applications that integrate with LinkedIn to share content, interact with connections, manage jobs and ads, and more.
In this comprehensive guide, we will cover how to use the LinkedIn API with JavaScript. We will go through the process of registering as a LinkedIn developer, obtaining API keys, making API requests, and handling responses. By the end, you will have the knowledge to start building JavaScript applications that leverage the power of LinkedIn’s platform.
Prerequisites
Before diving into working with the LinkedIn API, you should have:
- Basic knowledge of JavaScript fundamentals
- Familiarity with making HTTP requests in JavaScript
- A LinkedIn account and profile
We will be making API requests from the client-side in this tutorial. For real applications, you would make server-side requests to securely handle authentication. But the client-side calls are useful for learning and prototyping.
Obtaining API Keys
To start using the LinkedIn API, you need to register as a LinkedIn developer and obtain API keys. Here is the process:
- Go to https://www.linkedin.com/developers/ and log in with your LinkedIn account.
- Click on “My Apps” in the top menu bar and then “Create Application”.
- Fill out the form with your application name, description, logo, and other details.
- Agree to the API Terms of Use and click “Submit”.
- On the next page, copy down the Client ID and Client Secret values. These will serve as your API keys.
Keep these API keys private and do not share them publicly. We will use them later to make authenticated requests.
Making API Requests
Let’s start making some basic requests to the LinkedIn API. We will send API calls in JavaScript using the Fetch API.
Here is an example request to retrieve profile data for the authenticated user:
fetch('https://api.linkedin.com/v2/me', { headers: { 'Authorization': 'Bearer YOUR_ACCESS_TOKEN' } }) .then(response => response.json()) .then(data => { // handle data });
We make a GET request to the `/v2/me` endpoint, passing the access token in the Authorization header. This returns profile information for the currently authenticated user.
The steps are:
- Make a Fetch call to the API endpoint with any headers required for authentication.
- Convert the response into JSON format using the .json() method.
- Process the JSON data from the response.
Let’s look at a few more examples of API requests:
Get a user’s profile:
fetch('https://api.linkedin.com/v2/me', { headers: { 'Authorization': 'Bearer ACCESS_TOKEN' } })
Search for other profiles:
fetch('https://api.linkedin.com/v2/search?q=software&start=0&count=10&filters=resultType-NAMED_PEOPLE', { headers: { 'Authorization': 'Bearer ACCESS_TOKEN' } })
Share content on a user’s profile:
fetch('https://api.linkedin.com/v2/shares?owner=urn:li:person:USER_ID&content={'originalUrl':'https://example.com','title':'Hello World','description':'Greeting'}', { method: 'POST', headers: { 'Authorization': 'Bearer ACCESS_TOKEN' 'Content-Type': 'application/json' } })
As you can see, the LinkedIn API endpoints follow a RESTful design. You can GET profile data, POST shared content, send search queries, and more.
Authentication and Access Tokens
For most LinkedIn API requests, you will need to authenticate and provide an access token in the request headers.
There are a couple approaches to get an access token:
OAuth 2.0
You can implement OAuth 2.0 authorization code flow to securely obtain access tokens from LinkedIn. This is the preferred method, especially for production applications.
Here is a high-level overview:
- Redirect users to LinkedIn’s authorization URL with your client ID.
- LinkedIn will prompt users to log in and authorize access.
- LinkedIn redirects back with an authorization code if approved.
- Exchange the authorization code for an access token by making a server-side request.
- You can now use the access token to make API requests.
Requesting Access Tokens
For testing, you can obtain an access token by making this Fetch request:
fetch('https://www.linkedin.com/oauth/v2/accessToken', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET' }) .then(response => response.json()) .then(data => { // `data` contains access token });
This posts the client ID and secret to LinkedIn’s token endpoint. The response contains the access token value.
Use this access token in the Authorization header for API requests that require authentication.
The access token will expire after a certain period. You will need to refresh the token to get a new valid one.
Sample Use Cases
Let’s explore some sample use cases for the LinkedIn API with JavaScript.
Profile Information
Get the profile for the authenticated user:
fetch('https://api.linkedin.com/v2/me', { headers: { 'Authorization': 'Bearer ACCESS_TOKEN' } }) .then(response => response.json()) .then(data => { // Log profile data console.log(data); // Display profile image let img = document.getElementById('profile-image'); img.src = data.profilePicture['displayImage~'].elements[3].identifiers[0].identifier; // Display full name let name = document.getElementById('full-name'); name.innerText = data.firstName + ' ' + data.lastName; });
This fetches the user’s profile and displays their photo, name, and other fields.
Searching Profiles
Search for other profiles matching a keyword:
const searchTerm = 'software developer'; fetch(`https://api.linkedin.com/v2/search?q=${searchTerm}&start=0&count=10&filters=resultType-NAMED_PEOPLE`, { headers: { 'Authorization': 'Bearer ACCESS_TOKEN' } }) .then(response => response.json()) .then(data => { // Display search results let results = document.getElementById('search-results'); data.results.forEach(item => { let img = document.createElement('img'); img.src = item.targetUrn; let text = document.createElement('div'); text.innerText = `${item.targetUrn} - ${item.title}`; let result = document.createElement('div'); result.append(img); result.append(text); results.append(result); }); });
This searches profiles and displays the name/photo for each result.
Posting Updates
Share content to the authenticated user’s profile:
const url = 'https://example.com'; const title = 'Hello World'; const description = 'Greeting'; fetch(`https://api.linkedin.com/v2/shares?owner=urn:li:person:USER_ID&content={'originalUrl':'${url}','title':'${title}','description':'${description}'}'`, { method: 'POST', headers: { 'Authorization': 'Bearer ACCESS_TOKEN' 'Content-Type': 'application/json' } }) .then(response => { if (response.ok) { console.log('Content shared!'); } else { console.error('Error sharing content'); } })
This shares a link and text to the user’s profile.
The LinkedIn API opens up many possibilities like this for interacting with LinkedIn data.
JavaScript SDK
LinkedIn provides an official JavaScript SDK that wraps the API into an easy-to-use library.
Rather than dealing with raw API calls and responses, you can use the SDK’s methods like:
linkedin.people.getMyProfile() linkedin.people.search() linkedin.feed.shareContent()
The SDK handles authentication, request formatting, response parsing, and more.
To use the SDK, include this script tag in your HTML:
<script src="https://api.linkedin.com/v2/js/linkedin-sdk.js"></script>
Then initialize it with your client ID:
linkedin.init({ client_id: 'YOUR_CLIENT_ID' });
Now you can call the SDK methods.
Here is an example getting the authenticated user’s profile:
linkedin.people.getMyProfile() .then(response => { // handle response data });
The SDK provides a higher-level interface to LinkedIn’s API. Under the hood, it is still making the same REST API calls.
Common Use Cases
Here are some common use cases for the LinkedIn API:
– **Profiles** – Get profile data, retrieve connections, view share statistics.
– **Search** – Search for people, jobs, companies, groups, schools.
– **Sharing** – Post shareable content to user profiles.
– **Network** – Manage connections and invitations.
– **Jobs** – Post, manage, promote job listings.
– **Advertising** – Manage ads and sponsored content.
– **Analytics** – Track page views, impression, shares.
– **Groups** – Integrate with LinkedIn groups.
– **Education** – Integrate with LinkedIn Learning courses.
– **Recruitment** – Leverage recruiting solutions.
The possibilities are vast with LinkedIn’s broad social network. Their API exposes valuable data and capabilities to developers.
Limitations
When working with the LinkedIn API, keep these limitations in mind:
– **Rate limiting** – Requests are throttled at a certain rate. If exceeded, requests start failing.
– **Restricted data** – Certain fields like email addresses and phone numbers are not available.
– **User authorization** – To post updates or manage user data, explicit authorization is required.
– **No inbound connections** – You cannot retrieve a list of the user’s inbound connections via the API.
– **Limited search depth** – Profile search is limited to retrieving only a few thousand results.
– **Client-side only** – API access tokens should only be used in server-side code for security.
Make sure to thoroughly test your application to avoid hitting rate limits in production. And follow LinkedIn’s guidelines for respecting user privacy and data.
Conclusion
The LinkedIn API opens up LinkedIn’s platform to developers, enabling you to tap into their extensive data and large network of professionals.
Their REST API provides diverse endpoints for profiles, sharing, messaging, jobs, analytics, and more. Authenticate with OAuth 2.0 and access tokens to start making requests.
Use their JavaScript SDK for a convenient wrapper around the raw API functionality.
Now you have the knowledge to start building powerful applications with LinkedIn integration using their comprehensive API.