Create posts
Learn how to publish Facebook posts, as a user or as a Page, with Unipile API.
Publish a post
To publish a post, use the Create a Post endpoint. A post needs a text or an attachment.
const { data } = await postsApi.createPost({
path: {
account_id: "acc_123456789",
},
body: {
text: "Hello from Facebook with Unipile!",
},
});post = posts_api.create_post(
"acc_123456789",
{"text": "Hello from Facebook with Unipile!"},
)curl --request POST \
--url https://api.unipile.com/v2/acc_123456789/posts \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"text": "Hello from Facebook with Unipile!"
}
'Publish as a Page
To publish as a Page the account administers, set post_as to its Page ID. The Page must be activated on the account.
const { data } = await postsApi.createPost({
path: {
account_id: "acc_123456789",
},
body: {
text: "Hello from our Page!",
post_as: "1357020157484056",
},
});post = posts_api.create_post(
"acc_123456789",
{
"text": "Hello from our Page!",
"post_as": "1357020157484056",
},
)curl --request POST \
--url https://api.unipile.com/v2/acc_123456789/posts \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"text": "Hello from our Page!",
"post_as": "1357020157484056"
}
'Choose the audience
Set can_read to choose who can see the post:
anyonemakes it public. This is the default.relations_onlylimits it to the account's friends.
Facebook does not support restricting comments: can_comment only accepts anyone, and any other value returns 400 api/invalid_parameters.
Add photos or a video
To add media, include files in the attachments array. A post takes either several images, displayed as an album, or one video. Images and a video can't be mixed.
Videos are processed asynchronously by Facebook. For large files, prefer multipart.
const pictureFile = await readFile("picture.jpg");
const { data } = await postsApi.createPost({
path: {
account_id: "acc_123456789",
},
body: {
text: "Hello from Facebook with Unipile!",
attachments: [
{
content: pictureFile.toString("base64"),
content_type: "image/jpeg",
filename: "picture.jpg",
},
],
},
});import base64
with open("picture.jpg", "rb") as f:
picture_file = base64.b64encode(f.read()).decode("utf-8")
post = posts_api.create_post(
"acc_123456789",
{
"text": "Hello from Facebook with Unipile!",
"attachments": [
{
"content": picture_file,
"content_type": "image/jpeg",
"filename": "picture.jpg",
},
],
},
)curl --request POST \
--url https://api.unipile.com/v2/acc_123456789/posts \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"text": "Hello from Facebook with Unipile!",
"attachments": [
{
"content": "base64",
"content_type": "image/jpeg",
"filename": "picture.jpg"
}
]
}
'Mention a user
To mention a person or a Page, insert @ followed by their ID or vanity name in the text. Facebook displays their name in the published post. An unknown mention returns 400 api/invalid_parameters.
const { data } = await postsApi.createPost({
path: {
account_id: "acc_123456789",
},
body: {
text: "Thanks to @johndoe for the help!",
},
});post = posts_api.create_post(
"acc_123456789",
{"text": "Thanks to @johndoe for the help!"},
)Mentions also work when editing posts, and in comments.
Share a post
To share a post, set quoted_post_id to its ID. Leave text empty for a simple repost. A share can't carry attachments.
const { data } = await postsApi.createPost({
path: {
account_id: "acc_123456789",
},
body: {
text: "Worth reading!",
quoted_post_id: "post_id",
},
});post = posts_api.create_post(
"acc_123456789",
{
"text": "Worth reading!",
"quoted_post_id": "post_id",
},
)curl --request POST \
--url https://api.unipile.com/v2/acc_123456789/posts \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"text": "Worth reading!",
"quoted_post_id": "post_id"
}
'
StoriesCreating Facebook stories is not supported.
Updated 16 days ago