Implementing Multi-User Social Account Linking with Ayrshare
If you’re building a SaaS platform or product that lets users post to their own social media accounts or get analytics on their social account, then the Ayrshare API multi-user capabilities are a great fit. You can let each of your users link and manage their own social profiles—Facebook, Instagram, LinkedIn, TikTok, YouTube, X, and more.
In this article, we’ll walk through how to implement a system where your users can authenticate and post to their own connected accounts using Ayrshare’s powerful multi-user integration features.
Ayrshare provides user profiles to manage users. You create and manage user profiles via Ayrshare’s API. Each profile gets it’s own Profile Key, which you include in your API calls, and each of your users can connect to their own social media accounts and post independently. All requests use your primary profile API key, and actions are scoped by profileKey, ensuring users can only interact with their own social data.
The following requires a business plan, so please contact us to find out more.
Step-by-Step Integration
We’ll go over all the individual steps of creating a user in Ayrshare, allowing them to connect their social account, and publishing a post. We recommend reviewing the integration workflow for a high-level overview.
Step 1: Create a User Profile
Each of your end-users should be associated with an Ayrshare Profile Key, which is important to securely store in your database. This key is used for all future actions for that user—posting, connecting social accounts, retrieving analytics, etc.
To create a profile, send a POST request to:
const axios = require('axios');
const createProfile = async () => {
const response = await axios.post(
'https://api.ayrshare.com/api/profiles',
{
profileName: 'john_shoes_001' // your internal user ID or name
},
{
headers: {
'Authorization': 'Bearer API_KEY',
'Content-Type': 'application/json'
}
}
);
const profileKey = response.data;
console.log('Profile Key Data:', profileKey);
};Step 2: Generate a Social Connect URL
Once the profile is created you will then want to allow this user to connect the social accounts. Start by generating a secure JWT URL, which returns a URL to open in a new tab or browser. This link is what your users will click to connect their own social accounts.
const getConnectUrl = async (profileKey) => {
const response = await axios.post(
'https://api.ayrshare.com/api/connect',
{
profileKey: profileKey,
redirectUrl: 'https://yourapp.com/social-connected' // optional
},
{
headers: {
'Authorization': 'Bearer API_KEY',
'Content-Type': 'application/json'
}
}
);
const connectUrl = response.data.url;
console.log('Connect URL:', connectUrl);
};You can open the URL in a new tab or modal window. After the user finishes connecting their social accounts, they’ll be redirected back to your app (if you specified redirectUrl as a parameter).
Step 3: Post on Behalf of Your User
Once a user has connected their accounts, you can begin publishing posts on their behalf. Just use their profileKey in the API request along with the API Key:
const postToSocial = async (profileKey) => {
await axios.post(
'https://app.ayrshare.com/api/post',
{
post: 'This is a test post from Ayrshare!',
platforms: ['twitter', 'linkedin', 'tiktok', 'instagram'],
mediaUrls: ["https://example.com/video.mp4"]
},
{
headers: {
'Authorization': `Bearer API_KEY`,
'Content-Type': 'application/json',
'Profile-Key': profileKey
}
}
);
};This will publish the post to the platforms the user connected. If the user hasn’t linked a platform, Ayrshare will simply skip it, but of course give you a warning.
Step 4: Check Social Accounts Connection Status
You can programmatically check the platforms a user has linked using the GET User API endpoint:
const checkConnections = async (profileKey) => {
const response = await axios.get('https://api.ayrshare.com/api/user', {
headers: {
'Authorization': `Bearer API_KEY`,
'Content-Type': 'application/json',
'Profile-Key': profileKey
}
});
console.log(response.data.connected);
};This helps you display which platforms are available for posting and prompt users to connect any missing ones.
Delete or Update Profiles
If a user deletes their account on your platform or you no longer have a need to offer social media management to that user, you should also delete their Ayrshare profile:
const deleteProfile = async (profileKey) => {
await axios.delete(
'https://api.ayrshare.com/api/profiles',
{
headers: {
'Authorization': `Bearer API_KEY`,
'Content-Type': 'application/json',
'Profile-Key': profileKey
}
}
);
};Note that deleting a user profile is irrevocable.
More Information
With Ayrshare’s multi-user integration, you can build a robust social media experience into your platform allowing each of your users to seamlessly connect and manage their own accounts.
For full docs, check out: