← Blog

Instagram Reels API vs YouTube Data API: how to upload a video from code

Guide ·

To upload a Reel from code, give Instagram a public URL for the video: create a media container, poll it until it’s finished, then publish it. YouTube works the other way round. You open a resumable session and send it the bytes yourself. The two also differ in sign-in, token lifetime, review and how they report failure, so code written for one barely carries over to the other.

socialized.dev runs those flows for YouTube, Instagram and Facebook: upload once, send one publish request, and follow one status and webhook lifecycle. It’s free for 10 videos a month.

Instagram Reels YouTube
Transfer Instagram pulls from your URL You push the bytes
API host graph.instagram.com www.googleapis.com
What you hold mid-upload A container ID A session URL
After an interruption Ask the container for its status Ask the session how far it got
What goes live You call media_publish The finished upload creates the video
Long-term grant A 60-day token you refresh A refresh token

The first row shapes your infrastructure. For Instagram you need somewhere public to host the file. For YouTube you need a process that can read the file and finish, or resume, a long request.

Instagram: the sign-in and token

This guide uses the Instagram API with Instagram Login. It needs a professional account (Business or Creator) and two permissions: instagram_business_basic and instagram_business_content_publish.

  1. Send the person to https://www.instagram.com/oauth/authorize with your app ID, redirect URI, those scopes, response_type=code and a random one-time state value bound to their session.
  2. On the callback, verify the returned state against that session first, then POST the returned code to https://api.instagram.com/oauth/access_token, with your app’s ID and secret, the redirect URI and grant_type=authorization_code. You get a short-lived token.
  3. Exchange it at GET https://graph.instagram.com/access_token, with your app secret and grant_type=ig_exchange_token, for a token that lasts 60 days.
  4. Before that runs out, call GET https://graph.instagram.com/refresh_access_token with grant_type=ig_refresh_token. The token must be at least 24 hours old and not yet expired.

There is no separate refresh token. If the 60 days lapse, the person has to connect again, so run the refresh on a schedule rather than waiting for your next publish.

Check two things at sign-in. The consent screen lets someone untick the publish permission, so read the permissions in the short-lived token’s response when it’s there. Then call /me?fields=user_id,username,account_type: it gives you the ID to publish to, and shows whether the account is professional.

Instagram: upload a Reel with a script

Instagram fetches video_url itself. It must be a public URL that stays reachable until the container has finished processing, so a local path or a link behind a sign-in won’t work. This script needs curl and jq:

set -euo pipefail
: "${IG_USER_ID:?}" "${IG_TOKEN:?}" "${VIDEO_URL:?}"
GRAPH=https://graph.instagram.com

# 1. Create a container. Instagram starts fetching the video.
CONTAINER_ID=$(curl -sf "$GRAPH/$IG_USER_ID/media" \
  -H "Authorization: Bearer $IG_TOKEN" \
  -d media_type=REELS \
  -d share_to_feed=true \
  --data-urlencode "video_url=$VIDEO_URL" \
  --data-urlencode "caption=Launch day." | jq -r .id)
echo "container: $CONTAINER_ID" >&2

# 2. Poll until Instagram has processed it.
STATUS=IN_PROGRESS
for _ in 1 2 3 4 5; do
  sleep 60
  STATUS=$(curl -sf "$GRAPH/$CONTAINER_ID?fields=status_code" \
    -H "Authorization: Bearer $IG_TOKEN" | jq -r .status_code)
  case "$STATUS" in
    FINISHED | ERROR | EXPIRED) break ;;
  esac
done
if [ "$STATUS" != FINISHED ]; then
  echo "not published: container is $STATUS" >&2
  exit 1
fi

# 3. Publish it. The answer holds the new media ID.
curl -sf "$GRAPH/$IG_USER_ID/media_publish" \
  -H "Authorization: Bearer $IG_TOKEN" \
  -d creation_id="$CONTAINER_ID"

ERROR means Instagram couldn’t process the video. EXPIRED means the container sat unpublished for a day. For the post’s address, read /{media-id}?fields=permalink. You can also pass a public image as cover_url when you create the container.

Be careful with the last call. If it times out, you don’t know whether the Reel went live. Store the container ID, and don’t recover by creating a new container: that’s how one video gets posted twice.

A Reel has to be 3 seconds to 15 minutes long and no more than 300 MB, with a caption of at most 2,200 characters. There is no title field.

YouTube: push the bytes into a session

You need a Google Cloud project with the YouTube Data API v3 enabled. Uploading needs the https://www.googleapis.com/auth/youtube.upload scope; add youtube.readonly to read the channel and the video’s processing status. Request access_type=offline and prompt=consent to receive a refresh token, and exchange it at https://oauth2.googleapis.com/token for new access tokens.

The upload starts with metadata. Google answers with a session URL in the Location header, and the bytes go there:

set -euo pipefail
: "${YT_TOKEN:?}"
SIZE=$(wc -c < clip.mp4 | tr -d ' ')
UPLOAD=https://www.googleapis.com/upload/youtube/v3/videos

# 1. Open a session. metadata.json holds the snippet and status objects.
SESSION=$(curl -sf -X POST \
  "$UPLOAD?uploadType=resumable&part=snippet,status" \
  -H "Authorization: Bearer $YT_TOKEN" \
  -H "Content-Type: application/json" \
  -H "X-Upload-Content-Type: video/mp4" \
  -H "X-Upload-Content-Length: $SIZE" \
  --data-binary @metadata.json \
  -D - -o /dev/null |
  awk 'tolower($1) == "location:" { print $2 }' | tr -d '\r')
echo "session: $SESSION" >&2

# 2. Send the file. A completed upload answers with the video resource.
curl -sf -X PUT "$SESSION" \
  -H "Authorization: Bearer $YT_TOKEN" \
  -H "Content-Type: video/mp4" \
  --upload-file clip.mp4

A metadata.json for it:

{
  "snippet": { "title": "Launch day", "description": "What shipped." },
  "status": { "privacyStatus": "private", "selfDeclaredMadeForKids": false }
}

If the connection drops, don’t start again. Ask the session what it has, with an empty PUT:

curl -i -X PUT "$SESSION" \
  -H "Authorization: Bearer $YT_TOKEN" \
  -H "Content-Range: bytes */$SIZE" \
  -H "Content-Length: 0"

A 200 or 201 means the upload is already complete. A 308 means it isn’t: Range: bytes=0-N says the first N + 1 bytes arrived, so continue from byte N + 1 with a matching Content-Range. With no Range header, send from the start. If you upload in chunks, make every chunk but the last a multiple of 256 KiB.

A finished upload still isn’t a processed video. Read GET /youtube/v3/videos?part=status,processingDetails&id={videoId} and look at status.uploadStatus and processingDetails.processingStatus. YouTube can accept the bytes and reject the video afterwards.

Tokens and review gates compared

YouTube access tokens expire quickly, so use expires_in and refresh them. A refresh doesn’t return a new refresh token: keep the one you have. It can still stop working. With an external OAuth app left in “Testing”, it expires after 7 days; it also dies after six months unused, or when the grant is revoked. The upload and read scopes are sensitive, so until Google verifies your app only 100 people can grant them.

Instagram gives you one 60-day token, which you can only refresh while it’s alive. To publish for accounts without a role on your Meta app, each permission has to pass App Review, with a written use case and a screencast showing it from sign-in to result.

These are two separate reviews, run by two companies, and approval from one does nothing for the other.

Common failure modes

You see What happened What to do
Instagram container ERROR The video couldn’t be processed Check the file and that its URL is public
Instagram container EXPIRED It wasn’t published within a day Create a new container
Meta OAuthException, code 190 The grant is no longer valid Have the person reconnect
Meta codes such as 4, 17 or 613 Throttling Wait and back off
Google invalid_grant The refresh token is dead Have the person reconnect
YouTube 401 youtubeSignupRequired That Google identity has no channel Reconnect with the one that owns it
YouTube quotaExceeded The daily quota is spent Retry after midnight Pacific time
YouTube uploadLimitExceeded The channel hit its upload limit Retry later
YouTube rejectionReason: length The channel can’t take a video that long Verify the channel and upload again
YouTube 308 The upload is incomplete, not failed Resume from the Range header

youtubeSignupRequired deserves a special note: a token refresh can succeed while every upload returns it, for example after a channel moves to another Google account. A working refresh doesn’t prove the grant can still publish.

Both uploads behind one lifecycle

With socialized.dev, you connect each account once in the dashboard. Your code then uploads the video as an asset and sends one publish request. The CLI does the upload with socialized.dev assets upload --file clip.mp4; over HTTP, the quickstart shows the multipart steps.

curl -X POST \
  "https://api.socialized.dev/api/v1/brands/$BRAND_ID/publications" \
  -H "Authorization: Bearer $SOCIALIZED_DEV_API_KEY" \
  -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "videoAssetId": "'"$ASSET_ID"'",
    "title": "Launch day",
    "description": "What shipped.",
    "targets": [
      { "platform": "instagram" },
      { "platform": "youtube", "options": { "privacyStatus": "private" } }
    ]
  }'

Behind that request, each destination runs on its own:

  • Instagram gets a public media URL for your upload. socialized.dev creates the REELS container, polls it for about five minutes, and publishes it once it’s finished.
  • YouTube gets the bytes from storage through a resumable session, in chunks. Before each one, socialized.dev asks YouTube how much it has. Afterwards it watches processing for a short while, so published doesn’t promise YouTube has finished processing. A rejection seen in that window fails the destination.
  • Tokens are refreshed for you. If a grant dies, someone reconnects it in the dashboard, and the connection.reauth_required webhook tells you when.

Both destinations move through pending and processing to published, with an externalUrl when the platform gives one, or failed, with a failure.kind such as reconnect, quota, file or busy. The publication ends as done, partial or failed. A video that breaks a known limit is refused up front with 422 VideoNotAcceptedError.

Reuse the Idempotency-Key if you have to repeat the same request: for 24 hours it returns the same publication. A webhook saves you polling. This is the body when one destination goes live:

{
  "event": "publication.target.published",
  "brandId": "brand_…",
  "data": {
    "publicationId": "pub_…",
    "targetId": "tgt_…",
    "platform": "youtube",
    "externalPostId": "…",
    "externalUrl": "https://youtu.be/…",
    "warnings": []
  }
}

You also get publication.target.failed and publication.completed. Verify the X-Socialized-Signature header against the raw body, and use X-Socialized-Event to ignore repeat deliveries.

Which should you use?

Call the APIs directly if you only need one platform, need features outside video publishing, or don’t want another service holding your account grants. socialized.dev has to hold them to publish for you.

Use socialized.dev if you want the same video on both without running two token lifecycles, two upload state machines and two reviews. For Facebook Pages as well, read the three-platform publishing guide, or see the developers page.

Publish it once. See where it went live.

One upload to YouTube, Instagram and Facebook, from the dashboard, the API or the CLI. Free for 10 videos a month, no card needed.