Understanding the Ayrshare Auto-Schedule API Endpoint

[ad_1]
Ayrshare offers editing of posts to all social networks via API, which is one of the most used capabilities of the system. Little is known auto-schedule
An API endpoint can take your programming to the next level.
The automatic schedule endpoint allows users to automatically schedule their posts at predefined times and dates for posting using Ayrshare’s social media API.
Why would you want to do this? You may have certain times when your business posts have the most engagement (eg 9 AM), or you want to exclude certain days (New Years), or you want to prevent more than 5 posts per day (the automatic scheduler will find the next available time if they are all taken in a day).
This guide walks through the process of using the Ayrshare scheduler endpoint with examples and JavaScript code samples.
Understanding the Endpoint of Automated Programming
The automatic schedule endpoint allows you to define specific times throughout the week when you want your content to be automatically posted. You can create multiple schedules up to a social network, giving you more control over your social media strategy.
For example, you can set a schedule to only post at 9 AM, 11 AM, and 2 PM on weekdays, except for New Years, on Facebook, YouTube, TikTok, and Instagram.
To use the default scheduler endpoint, you’ll need a few things:
- Ayrshare account: Set up a Premium or Business plan to enable automatic scheduling functionality.
- API key: Find your unique API key on the Ayrshare Developer Dashboard.
- Profile Key: Also find the profile key on the dashboard if you are editing a program for one of your users.
Setting Up Your Automatic Schedule
Ayrshare uses a two-step process for automatic scheduling. Here’s how it works:
- Define Your Schedule: Use a POST request to
/api/auto-schedule/set
endpoint. In the body of the request, you will specify the times (in UTC format) that you want your post to be published. You can also assign a title to your program for easy reference.
Sample code:
const apiKey = 'YOUR_API_KEY';
const profileKey = 'YOUR_PROFILE_KEY';
async function createSchedule(scheduleTitle, times) {
const url = 'https://app.ayrshare.com/api/auto-schedule/set';
const data = {
times: times.map(time => time + 'Z'), // Ensure times are in UTC format
title: scheduleTitle
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'Profile-Key': profileKey
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error(`Error creating schedule: ${response.statusText}`);
}
const data = await response.json();
console.log(`Schedule created successfully! Title: ${data.title}`);
}
// Example Usage
const myScheduleTimes = ['13:05', '20:14'];
createSchedule('My Afternoon Schedule', myScheduleTimes);
This code snippet creates a schedule with posting times of 1:05 PM and 8:14 PM UTC, titled “My Afternoon Schedule.”
- Use a schedule in your post: Once you’ve defined your schedule, you can refer to it when creating posts using
/post
endpoint. set theautoSchedule
parameter totrue
and include the title of your desired schedule (the one you created in step 1) within the body of the request.
Sample code:
const apiKey = 'YOUR_API_KEY';
const profileKey = 'YOUR_PROFILE_KEY';
async function createPost(postText, platforms, scheduleTitle) {
const url = 'https://app.ayrshare.com/api/post';
const data = {
postText,
platforms,
autoSchedule: {
schedule: true,
title: scheduleTitle
}
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'Profile-Key': profileKey
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error(`Error creating post: ${response.statusText}`);
}
const data = await response.json();
console.log(`Post created successfully! ID: ${data.id}`);
}
// Example Usage
const postText = 'This post will be published according to my Afternoon Schedule';
const platforms = ['facebook'];
const scheduleTitle = 'My Afternoon Schedule';
createPost(postText, platforms, scheduleTitle);
This code creates a new Facebook post with the text “This post will be published according to my Afternoon Schedule” and refers to the “My Afternoon Schedule” you defined earlier. Ayrshare will automatically queue the post for the next available time within the schedule you have selected.
Advantages of Auto-Editing
Automated scheduling offers several benefits:
- Save Time: Plan your posts ahead of time and free yourself from manual posting from time to time. Go ahead and line up several posts at once.
- Similar Shipping: Maintain a consistent presence on social media by ensuring a consistent flow of content.
More Control
In addition to the time settings, you specify which days of the week you want to post and even set specific dates to skip. For example you can set posts to only go out during the week, and skip New Year’s Day. Just use the daysOfWeek
again exludeDates
boundaries.
Whether you’re planning a single post or multiple posts, the examples provided should help you get started quickly. Good planning!
Remember: Check out the Ayrshare social media automation documentation for the latest API endpoint details and updates.