Thursday, June 13, 2024

Understanding the Ayrshare Auto-Schedule API Endpoint

-

Ayrshare offers post scheduling to all the social networks via an API, which is one of the most used capabilities of the system. The lesser known auto-schedule API endpoint can take your scheduling to the next level.

The auto-schedule endpoint enables users to automate the scheduling of their posts with set pre-defined posting times and days using the Ayrshare social media API.

Why would you want to do this? You may have specific times when your business posts have the most engagement (e.g. 9 AM), or want to exclude certain days (New Years), or even want to prevent more than 5 post per day (the auto-scheduler will find the next available time slot if all are taken for the day).

This guide goes through the process of using the Ayrshare auto-schedule endpoint with examples and JavaScript code samples.

Understanding the Auto-Schedule Endpoint

The auto-schedule endpoint lets you define specific times throughout the week when you want your content to be automatically posted. You can create multiple schedules down to the social network, giving you granular control over your social media strategy.

For example, you can set up a schedule to only post a 9 AM, 11 AM, and 2 PM only on weekdays, excluding New Years, for Facebook, YouTube, TikTok, and Instagram.

To use the auto-schedule endpoint, you’ll need a few things:

  • Ayrshare Account: Set up a Premium or Business plan to unlock auto-scheduling functionality.
  • API Key: Get your unique API key from the Ayrshare Developer Dashboard.
  • Profile Key: Obtain the profile key also from the dashboard if you are setting up the schedule for one of your users.

Setting Up Your Auto-Schedule

Ayrshare utilizes a two-step process for auto-scheduling. Here’s how it works:

  1. Define Your Schedule: Use a POST request to the /api/auto-schedule/set endpoint. In the request body, you’ll specify the times (in UTC format) when you want your posts to be published. You can also assign a title to your schedule for easy reference.

Code Sample:

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 at 1:05 PM and 8:14 PM UTC, titled “My Afternoon Schedule.”

  1. Use the Schedule in Your Posts: Once you’ve defined your schedule, you can reference it when creating posts using the /post endpoint. Set the autoSchedule parameter to true and include the title of your desired schedule (the one you created in step 1) within the request body.

Code Sample:

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 post for Facebook with the text “This post will be published according to my Afternoon Schedule” and references the “My Afternoon Schedule” you defined earlier. Ayrshare will automatically queue the post for the next available time slot within your chosen schedule.

Benefits of Auto-Scheduling

Auto-scheduling offers several advantages:

  • Saves Time: Schedule your posts in advance and free yourself from manually posting at specific times. Go ahead and queue up a few dozen posts all at once.
  • Consistent Posting: Maintain a consistent presence on social media by ensuring regular content flow.

Even More Control

In addition to settings times, you specify which days of the week you want to post on and even set specific dates to skip. For example you can set posts to only go out on weekdays, and skip New Years Day. Just use the daysOfWeek and exludeDates parameters.

Whether you’re scheduling a single post or multiple posts, the provided examples should help you get started quickly. Happy scheduling!

Remember: Refer to the Ayrshare documentation on auto scheduling for social media for the latest API endpoint details and updates.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Stories