Search...
createDraftPost( )
Creates a draft post.
Description
The draft post's memberId
is required for third-party apps.
Admin Method
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Authorization
Request
This endpoint does not take any parameters
Response Object
NAME
TYPE
DESCRIPTION
draftPost
DraftPost
Created draft post info.
Status/Error Codes
Was this helpful?
Create a draft post (export from the backend code)
Copy Code
1import { draftPosts } from 'wix-blog-backend';2import { webMethod, Permissions } from 'wix-web-module';3import { elevate } from 'wix-auth';45 /* Sample draftPost value:6 * {7 * "categoryIds": [],8 * "commentingEnabled": true,9 * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.",10 * "featured": true,11 * "language": "en",12 * "media": {13 * "custom": false,14 * "displayed": true15 * },16 * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",17 * "previewTextParagraph": 2,18 * "pricingPlanIds": [],19 * "relatedPostIds": [],20 * "richContent": {21 * "nodes": [22 * {23 * "type": "PARAGRAPH",24 * "id": "pvirv1",25 * "nodes": [26 * {27 * "type": "TEXT",28 * "id": "",29 * "nodes": [],30 * "textData": {31 * "text": "Hello world",32 * "decorations": []33 * }34 * }35 * ],36 * "paragraphData": {}37 * }38 * ],39 * }40 * "seoData": {41 * "tags": []42 * "},43 * "seoSlug": "on-our-side-1",44 * "tagIds": [],45 * "title": "On our side.",46 * }47 */4849const elevatedCreateDraftPost = elevate(draftPosts.createDraftPost);5051export const myCreateDraftPostFunction = webMethod(52 Permissions.Admin,53 async (draftPost) => {54 try {55 const createdDraftPost = await elevatedCreateDraftPost(draftPost);56 console.log("Successfully created the following draft post":", createdDraftPost);57 return createdDraftPost;58 } catch (error) {59 console.error(error);60 // Handle the error61 }62 }63);6465/* Promise resolves to:66 * {67 * "_id": "9bf5b0c2-3e06-499f-9c35-7e1b2ba91ae8",68 * "_createdDate": "2024-01-23T10":53":19.658Z",69 * "categoryIds": [],70 * "changeOrigin": "MANUAL_SAVE",71 * "commentingEnabled": true,72 * "contentId": "65af9a9ff178881294652e92",73 * "editedDate": "2024-01-23T10":53":19.658Z",74 * "editingSessionId": "7e79aa62-c68a-4b24-b9be-ae07f5ada751",75 * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.",76 * "featured": true,77 * "hashtags": [],78 * "hasUnpublishedChanges": true,79 * "language": "en",80 * "media": {81 * "custom": false,82 * "displayed": true83 * },84 * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",85 * "minutesToRead": 0,86 * "mostRecentContributorId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",87 * "paidContentParagraph": 2,88 * "pricingPlanIds": [],89 * "previewTextParagraph": 2,90 * "relatedPostIds": [],91 * "richContent": {92 * "nodes": [93 * {94 * "type": "PARAGRAPH",95 * "id": "pvirv1",96 * "nodes": [97 * {98 * "type": "TEXT",99 * "id": "",100 * "nodes": [],101 * "textData": {102 * "text": "Hello world",103 * "decorations": []104 * }105 * }106 * ],107 * "paragraphData": {}108 * }109 * ],110 * }111 * "seoData": {112 * "settings": {113 * "keywords": [],114 * "preventAutoRedirect": false115 * },116 * "tags": []117 * },118 * "seoSlug": "on-our-side-1",119 * "slugs": [],120 * "status": "UNPUBLISHED",121 * "tagIds": [],122 * "title": "On our side.",123 * "translations": []124 * }125 */
Create a draft post using options (export from backend code)
Copy Code
1import { draftPosts } from 'wix-blog-backend';2import { webMethod, Permissions } from 'wix-web-module';3import { elevate } from 'wix-auth';45/* Sample draftPost value:6 * {7 * "categoryIds": [8 * "c8780752-f517-4cf9-9c18-0f9a22d00926",9 * "590635d7-cc7c-48cb-970c-f8339daa1cfe"10 * ],11 * "commentingEnabled": true,12 * "editingSessionId": "session1",13 * "excerpt": "Let"s begin with Philo, a first century Egyptian.",14 * "featured": false,15 * "firstPublishedDate": "2024-01-01",16 * "hashtags": [17 * "vacation",18 * "dream",19 * "summer",20 * "hashtag"21 * ],22 * "heroImage": "https"://example.com/hero-image.jpg",23 * "language": "en",24 * "media": {},25 * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",26 * "moderationDetails": {},27 * "previewTextParagraph": 2,28 * "pricingPlanIds": [],29 * "relatedPostIds": [30 * "57194b39-c4bc-45a6-a30d-f8e860a020b2",31 * "d655251a-74eb-467d-9beb-1962984b8d94"32 * ],33 * "richContent": {34 * "nodes": [35 * {36 * "type": "PARAGRAPH",37 * "id": "pvirv1",38 * "nodes": [39 * {40 * "type": "TEXT",41 * "id": "",42 * "nodes": [],43 * "textData": {44 * "text": "Hello world",45 * "decorations": []46 * }47 * }48 * ],49 * "paragraphData": {}50 * }51 * ],52 * },53 * "seoData": {},54 * "seoSlug": "rocket-man",55 * "tagIds": [56 * "a55b2c06-cbec-4d01-a8bb-cd7029056c75",57 * "d2b0c02b-72c1-45af-ba58-3520cec9abe3"58 * ],59 * "title": "Rocket Man",60 * };61 *62 * Sample options value":63 * {64 * "fieldsets": ["URL"],65 * "publish": false66 * };67 */6869const elevatedCreateDraftPost = elevate(draftPosts.createDraftPost);7071export const myCreateDraftPostFunction = webMethod(72 Permissions.Admin,73 async (draftPost) => {74 try {75 const createdDraftPost = await elevatedCreateDraftPost(draftPost);76 console.log("Successfully created the following draft post":", createdDraftPost);77 return createdDraftPost;78 } catch (error) {79 console.error(error);80 // Handle the error81 }82 }83);8485/* Promise resolves to":86 * {87 * "_id": "9124f6ef-a537-4bdd-bca3-e7306947deeb",88 * "_createdDate": "2024-01-16T10":22":50.071Z",89 * "categoryIds": [],90 * "changeOrigin": "PUBLISH",91 * "commentingEnabled": true,92 * "contentId": "65a658fb28062c88564addfa",93 * "editingSessionId": "95091d24-7d76-45d1-920b-6dc0b0b45eaa",94 * "editedDate": "2024-01-16T10":22":50.071Z",95 * "excerpt": "Let"s begin with Philo, a first century Egyptian.",96 * "featured": false,97 * "firstPublishedDate": "2024-01-16T10":22":50.139Z",98 * "hashtags": [],99 * "hasUnpublishedChanges": false,100 * "language": "en",101 * "media": {102 * "custom": false,103 * "displayed": true104 * },105 * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",106 * "minutesToRead": 10,107 * "mostRecentContributorId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",108 * "paidContentParagraph": 2,109 * "pricingPlanIds": [],110 * "previewTextParagraph": 2,111 * "relatedPostIds": [112 * "57194b39-c4bc-45a6-a30d-f8e860a020b2",113 * "d655251a-74eb-467d-9beb-1962984b8d94"114 * ],115 * "richContent": {116 * "nodes": [117 * {118 * "type": "PARAGRAPH",119 * "id": "pvirv1",120 * "nodes": [121 * {122 * "type": "TEXT",123 * "id": "",124 * "nodes": [],125 * "textData": {126 * "text": "Hello world",127 * "decorations": []128 * }129 * }130 * ],131 * "paragraphData": {}132 * }133 * ],134 * },135 * "seoData": {136 * "tags": []137 * },138 * "seoSlug": "on-our-side",139 * "slugs": [140 * "on-our-side"141 * ],142 * "status": "PUBLISHED",143 * "tagIds": [],144 * "title": "Rocket Man",145 * "translations": []146 * }147 */
Create a draft post (dashboard page code)
Copy Code
1import { draftPosts } from 'wix-blog-backend';23/* Sample draftPost value:4 * {5 * "categoryIds": [],6 * "commentingEnabled": true,7 * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.",8 * "featured": true,9 * "language": "en",10 * "media": {11 * "custom": false,12 * "displayed": true13 * },14 * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",15 * "previewTextParagraph": 2,16 * "pricingPlanIds": [],17 * "relatedPostIds": [],18 * "richContent": {19 * "nodes": [20 * {21 * "type": "PARAGRAPH",22 * "id": "pvirv1",23 * "nodes": [24 * {25 * "type": "TEXT",26 * "id": "",27 * "nodes": [],28 * "textData": {29 * "text": "Hello world",30 * "decorations": []31 * }32 * }33 * ],34 * "paragraphData": {}35 * }36 * ],37 * }38 * "seoData": {39 * "tags": []40 * "},41 * "seoSlug": "on-our-side-1",42 * "tagIds": [],43 * "title": "On our side.",44 * }45 */4647export async function myCreateDraftPostFunction(draftPost) {48 try {49 const createdDraftPost = await draftPosts.createDraftPost(draftPost);50 console.log('Successfully created the following draft post:', createdDraftPost);51 return createdDraftPost;52 } catch (error) {53 console.error(error);54 // Handle the error55 }56 }5758/* Promise resolves to:59 * {60 * "_id": "9bf5b0c2-3e06-499f-9c35-7e1b2ba91ae8",61 * "_createdDate": "2024-01-23T10":53":19.658Z",62 * "categoryIds": [],63 * "changeOrigin": "MANUAL_SAVE",64 * "commentingEnabled": true,65 * "contentId": "65af9a9ff178881294652e92",66 * "editedDate": "2024-01-23T10":53":19.658Z",67 * "editingSessionId": "7e79aa62-c68a-4b24-b9be-ae07f5ada751",68 * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.",69 * "featured": true,70 * "hashtags": [],71 * "hasUnpublishedChanges": true,72 * "language": "en",73 * "media": {74 * "custom": false,75 * "displayed": true76 * },77 * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",78 * "minutesToRead": 0,79 * "mostRecentContributorId": "c00e8a5c-322b-4e77-8813-002e3ea7e811",80 * "paidContentParagraph": 2,81 * "pricingPlanIds": [],82 * "previewTextParagraph": 2,83 * "relatedPostIds": [],84 * "richContent": {85 * "nodes": [86 * {87 * "type": "PARAGRAPH",88 * "id": "pvirv1",89 * "nodes": [90 * {91 * "type": "TEXT",92 * "id": "",93 * "nodes": [],94 * "textData": {95 * "text": "Hello world",96 * "decorations": []97 * }98 * }99 * ],100 * "paragraphData": {}101 * }102 * ],103 * }104 * "seoData": {105 * "settings": {106 * "keywords": [],107 * "preventAutoRedirect": false108 * },109 * "tags": []110 * },111 * "seoSlug": "on-our-side-1",112 * "slugs": [],113 * "status": "UNPUBLISHED",114 * "tagIds": [],115 * "title": "On our side.",116 * "translations": []117 * }118 */
Create a post and send an email notification to subscribers
This example uses triggered emails. Create a triggered email before trying this example.
Copy Code
1/*****************************************2 * Backend code - my-backend-file.web.js *3 *****************************************/45import { draftPosts } from 'wix-blog-backend';6import { contacts, triggeredEmails } from 'wix-crm-backend';7import { webMethod, Permissions } from 'wix-web-module';8import { elevate } from 'wix-auth';910// Create a draft post1112export const myCreateDraftPostFunction = webMethod(13 Permissions.Anyone,14 async (draftPost, options) => {15 try {16 const elevatedCreateDraftPost = elevate(draftPosts.createDraftPost);17 const createdDraftPost = await elevatedCreateDraftPost(draftPost, options);18 return createdDraftPost;19 } catch (error) {20 console.error(error);21 // Handle the error22 }23 }24);2526// Send an email notification to all subscribers2728export const mySendEmailToContactFunction = webMethod(29 Permissions.Anyone,30 async (postLink) => { // Pass the post URL from the onPostCreated event in events.js code31 let contactsInfo = []; // An array to contain the IDs and names of all subscribers32 const queryResults = await contacts.queryContacts()33 .eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "SUBSCRIBED") // Retrieve all subscribers34 .find();35 const subscribedContacts = queryResults.items;3637 if (subscribedContacts.length >= 1) {38 for(let i=0; i < subscribedContacts.length; i++) {39 const contactId = subscribedContacts[i]._id;40 const contactName = subscribedContacts[i].info.name.first;41 const contactObject = {42 id: contactId,43 name: contactName44 }; // Loop through the subscribedContacts object. Retrieve the ID and name of each contact from the object. Save each contact as a separate object, so the contact can be added to an array.45 contactsInfo.push(contactObject); // Push object to an array46 }47 } else {48 console.log('No contacts found');49 // Handle when no contacts are found50 }5152 const triggeredEmailTemplate = 'myEmail'; // Use your email template name from Triggered Emails5354 for (const contact of contactsInfo) { // Send each contact a notification email55 const options = {56 "variables": {57 "name": contact.name, // Save the contact name to use it in the triggered email template58 "link": postLink // Save the post URL to use it in the triggered template59 }60 };6162 try {63 await triggeredEmails.emailContact(triggeredEmailTemplate, contact.id, options);64 console.log('Email sent to a contact');65 } catch (error) {66 console.error(error);67 // Handle the error68 }69 }70 });7172/*****************************************73 * Backend code - events.js *74 *****************************************/7576import { mySendEmailToContactFunction } from 'backend/my-backend-file.web.js'7778export function wixBlog_onPostCreated(event) { // set the event that fires when a post is created79 const postLink = event.entity.url // save the post URL from the event payload80 mySendEmailToContactFunction(postLink) // send an email notification when a post is created81}8283/*************84 * Page code *85 *************/8687import { myCreateDraftPostFunction } from 'backend/my-backend-file.web.js'8889const post = {90 "title": "Hello, world!",91 "commentingEnabled": true,92 "featured": true,93 "language": "en",94 "memberId": "8a8b9b73-4da8-47a5-8268-4396e68a0605",95 "richContent": {96 "nodes": [97 {98 "type": "PARAGRAPH",99 "id": "pvirv1",100 "nodes": [101 {102 "type": "TEXT",103 "id": "",104 "nodes": [],105 "textData": {106 "text": "Hello, world.",107 "decorations": []108 }109 }110 ],111 "paragraphData": {}112 }113 ]114 }115}116117const options = {118 "publish": true, // Publish a post right after creating it119 "fieldsets": ["URL"] // Define this fieldset to receive the URL in the payload120}121122$w.onReady(function () {123124myCreateDraftPostFunction(post, options).then(() => {125 console.log("Created post") // create a post126 })127128})129130