> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: createDraftPost(draftPost: DraftPost, options: CreateDraftPostOptions) # Method package: wixBlogBackend # Method menu location: wixBlogBackend --> draftPosts --> createDraftPost # Method Link: https://dev.wix.com/docs/velo/apis/wix-blog-backend/draft-posts/create-draft-post.md # Method Description: Creates a draft post. The draft post's `memberId` is required for third-party apps. # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Create a draft post (export from the backend code) ```javascript import { draftPosts } from 'wix-blog-backend'; import { webMethod, Permissions } from 'wix-web-module'; import { elevate } from 'wix-auth'; /* Sample draftPost value: * { * "categoryIds": [], * "commentingEnabled": true, * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.", * "featured": true, * "language": "en", * "media": { * "custom": false, * "displayed": true * }, * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "previewTextParagraph": 2, * "pricingPlanIds": [], * "relatedPostIds": [], * "richContent": { * "nodes": [ * { * "type": "PARAGRAPH", * "id": "pvirv1", * "nodes": [ * { * "type": "TEXT", * "id": "", * "nodes": [], * "textData": { * "text": "Hello world", * "decorations": [] * } * } * ], * "paragraphData": {} * } * ], * } * "seoData": { * "tags": [] * "}, * "seoSlug": "on-our-side-1", * "tagIds": [], * "title": "On our side.", * } */ const elevatedCreateDraftPost = elevate(draftPosts.createDraftPost); export const myCreateDraftPostFunction = webMethod( Permissions.Admin, async (draftPost) => { try { const createdDraftPost = await elevatedCreateDraftPost(draftPost); console.log("Successfully created the following draft post":", createdDraftPost); return createdDraftPost; } catch (error) { console.error(error); // Handle the error } } ); /* Promise resolves to: * { * "_id": "9bf5b0c2-3e06-499f-9c35-7e1b2ba91ae8", * "_createdDate": "2024-01-23T10":53":19.658Z", * "categoryIds": [], * "changeOrigin": "MANUAL_SAVE", * "commentingEnabled": true, * "contentId": "65af9a9ff178881294652e92", * "editedDate": "2024-01-23T10":53":19.658Z", * "editingSessionId": "7e79aa62-c68a-4b24-b9be-ae07f5ada751", * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.", * "featured": true, * "hashtags": [], * "hasUnpublishedChanges": true, * "language": "en", * "media": { * "custom": false, * "displayed": true * }, * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "minutesToRead": 0, * "mostRecentContributorId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "paidContentParagraph": 2, * "pricingPlanIds": [], * "previewTextParagraph": 2, * "relatedPostIds": [], * "richContent": { * "nodes": [ * { * "type": "PARAGRAPH", * "id": "pvirv1", * "nodes": [ * { * "type": "TEXT", * "id": "", * "nodes": [], * "textData": { * "text": "Hello world", * "decorations": [] * } * } * ], * "paragraphData": {} * } * ], * } * "seoData": { * "settings": { * "keywords": [], * "preventAutoRedirect": false * }, * "tags": [] * }, * "seoSlug": "on-our-side-1", * "slugs": [], * "status": "UNPUBLISHED", * "tagIds": [], * "title": "On our side.", * "translations": [] * } */ ``` ## Create a draft post using options (export from backend code) ```javascript import { draftPosts } from 'wix-blog-backend'; import { webMethod, Permissions } from 'wix-web-module'; import { elevate } from 'wix-auth'; /* Sample draftPost value: * { * "categoryIds": [ * "c8780752-f517-4cf9-9c18-0f9a22d00926", * "590635d7-cc7c-48cb-970c-f8339daa1cfe" * ], * "commentingEnabled": true, * "editingSessionId": "session1", * "excerpt": "Let"s begin with Philo, a first century Egyptian.", * "featured": false, * "firstPublishedDate": "2024-01-01", * "hashtags": [ * "vacation", * "dream", * "summer", * "hashtag" * ], * "heroImage": "https"://example.com/hero-image.jpg", * "language": "en", * "media": {}, * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "moderationDetails": {}, * "previewTextParagraph": 2, * "pricingPlanIds": [], * "relatedPostIds": [ * "57194b39-c4bc-45a6-a30d-f8e860a020b2", * "d655251a-74eb-467d-9beb-1962984b8d94" * ], * "richContent": { * "nodes": [ * { * "type": "PARAGRAPH", * "id": "pvirv1", * "nodes": [ * { * "type": "TEXT", * "id": "", * "nodes": [], * "textData": { * "text": "Hello world", * "decorations": [] * } * } * ], * "paragraphData": {} * } * ], * }, * "seoData": {}, * "seoSlug": "rocket-man", * "tagIds": [ * "a55b2c06-cbec-4d01-a8bb-cd7029056c75", * "d2b0c02b-72c1-45af-ba58-3520cec9abe3" * ], * "title": "Rocket Man", * }; * * Sample options value": * { * "fieldsets": ["URL"], * "publish": false * }; */ const elevatedCreateDraftPost = elevate(draftPosts.createDraftPost); export const myCreateDraftPostFunction = webMethod( Permissions.Admin, async (draftPost) => { try { const createdDraftPost = await elevatedCreateDraftPost(draftPost); console.log("Successfully created the following draft post":", createdDraftPost); return createdDraftPost; } catch (error) { console.error(error); // Handle the error } } ); /* Promise resolves to": * { * "_id": "9124f6ef-a537-4bdd-bca3-e7306947deeb", * "_createdDate": "2024-01-16T10":22":50.071Z", * "categoryIds": [], * "changeOrigin": "PUBLISH", * "commentingEnabled": true, * "contentId": "65a658fb28062c88564addfa", * "editingSessionId": "95091d24-7d76-45d1-920b-6dc0b0b45eaa", * "editedDate": "2024-01-16T10":22":50.071Z", * "excerpt": "Let"s begin with Philo, a first century Egyptian.", * "featured": false, * "firstPublishedDate": "2024-01-16T10":22":50.139Z", * "hashtags": [], * "hasUnpublishedChanges": false, * "language": "en", * "media": { * "custom": false, * "displayed": true * }, * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "minutesToRead": 10, * "mostRecentContributorId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "paidContentParagraph": 2, * "pricingPlanIds": [], * "previewTextParagraph": 2, * "relatedPostIds": [ * "57194b39-c4bc-45a6-a30d-f8e860a020b2", * "d655251a-74eb-467d-9beb-1962984b8d94" * ], * "richContent": { * "nodes": [ * { * "type": "PARAGRAPH", * "id": "pvirv1", * "nodes": [ * { * "type": "TEXT", * "id": "", * "nodes": [], * "textData": { * "text": "Hello world", * "decorations": [] * } * } * ], * "paragraphData": {} * } * ], * }, * "seoData": { * "tags": [] * }, * "seoSlug": "on-our-side", * "slugs": [ * "on-our-side" * ], * "status": "PUBLISHED", * "tagIds": [], * "title": "Rocket Man", * "translations": [] * } */ ``` ## Create a draft post (dashboard page code) ```javascript import { draftPosts } from 'wix-blog-backend'; /* Sample draftPost value: * { * "categoryIds": [], * "commentingEnabled": true, * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.", * "featured": true, * "language": "en", * "media": { * "custom": false, * "displayed": true * }, * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "previewTextParagraph": 2, * "pricingPlanIds": [], * "relatedPostIds": [], * "richContent": { * "nodes": [ * { * "type": "PARAGRAPH", * "id": "pvirv1", * "nodes": [ * { * "type": "TEXT", * "id": "", * "nodes": [], * "textData": { * "text": "Hello world", * "decorations": [] * } * } * ], * "paragraphData": {} * } * ], * } * "seoData": { * "tags": [] * "}, * "seoSlug": "on-our-side-1", * "tagIds": [], * "title": "On our side.", * } */ export async function myCreateDraftPostFunction(draftPost) { try { const createdDraftPost = await draftPosts.createDraftPost(draftPost); console.log('Successfully created the following draft post:', createdDraftPost); return createdDraftPost; } catch (error) { console.error(error); // Handle the error } } /* Promise resolves to: * { * "_id": "9bf5b0c2-3e06-499f-9c35-7e1b2ba91ae8", * "_createdDate": "2024-01-23T10":53":19.658Z", * "categoryIds": [], * "changeOrigin": "MANUAL_SAVE", * "commentingEnabled": true, * "contentId": "65af9a9ff178881294652e92", * "editedDate": "2024-01-23T10":53":19.658Z", * "editingSessionId": "7e79aa62-c68a-4b24-b9be-ae07f5ada751", * "excerpt": "Knowing nothing of the early movement, it was a chase in the dark.", * "featured": true, * "hashtags": [], * "hasUnpublishedChanges": true, * "language": "en", * "media": { * "custom": false, * "displayed": true * }, * "memberId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "minutesToRead": 0, * "mostRecentContributorId": "c00e8a5c-322b-4e77-8813-002e3ea7e811", * "paidContentParagraph": 2, * "pricingPlanIds": [], * "previewTextParagraph": 2, * "relatedPostIds": [], * "richContent": { * "nodes": [ * { * "type": "PARAGRAPH", * "id": "pvirv1", * "nodes": [ * { * "type": "TEXT", * "id": "", * "nodes": [], * "textData": { * "text": "Hello world", * "decorations": [] * } * } * ], * "paragraphData": {} * } * ], * } * "seoData": { * "settings": { * "keywords": [], * "preventAutoRedirect": false * }, * "tags": [] * }, * "seoSlug": "on-our-side-1", * "slugs": [], * "status": "UNPUBLISHED", * "tagIds": [], * "title": "On our side.", * "translations": [] * } */ ``` ## Create a post and send an email notification to subscribers ```javascript /***************************************** * Backend code - my-backend-file.web.js * *****************************************/ import { draftPosts } from 'wix-blog-backend'; import { contacts, triggeredEmails } from 'wix-crm-backend'; import { webMethod, Permissions } from 'wix-web-module'; import { elevate } from 'wix-auth'; // Create a draft post export const myCreateDraftPostFunction = webMethod( Permissions.Anyone, async (draftPost, options) => { try { const elevatedCreateDraftPost = elevate(draftPosts.createDraftPost); const createdDraftPost = await elevatedCreateDraftPost(draftPost, options); return createdDraftPost; } catch (error) { console.error(error); // Handle the error } } ); // Send an email notification to all subscribers export const mySendEmailToContactFunction = webMethod( Permissions.Anyone, async (postLink) => { // Pass the post URL from the onPostCreated event in events.js code let contactsInfo = []; // An array to contain the IDs and names of all subscribers const queryResults = await contacts.queryContacts() .eq("info.extendedFields.emailSubscriptions.subscriptionStatus", "SUBSCRIBED") // Retrieve all subscribers .find(); const subscribedContacts = queryResults.items; if (subscribedContacts.length >= 1) { for(let i=0; i < subscribedContacts.length; i++) { const contactId = subscribedContacts[i]._id; const contactName = subscribedContacts[i].info.name.first; const contactObject = { id: contactId, name: contactName }; // 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. contactsInfo.push(contactObject); // Push object to an array } } else { console.log('No contacts found'); // Handle when no contacts are found } const triggeredEmailTemplate = 'myEmail'; // Use your email template name from Triggered Emails for (const contact of contactsInfo) { // Send each contact a notification email const options = { "variables": { "name": contact.name, // Save the contact name to use it in the triggered email template "link": postLink // Save the post URL to use it in the triggered template } }; try { await triggeredEmails.emailContact(triggeredEmailTemplate, contact.id, options); console.log('Email sent to a contact'); } catch (error) { console.error(error); // Handle the error } } }); /***************************************** * Backend code - events.js * *****************************************/ import { mySendEmailToContactFunction } from 'backend/my-backend-file.web.js' export function wixBlog_onPostCreated(event) { // set the event that fires when a post is created const postLink = event.entity.url // save the post URL from the event payload mySendEmailToContactFunction(postLink) // send an email notification when a post is created } /************* * Page code * *************/ import { myCreateDraftPostFunction } from 'backend/my-backend-file.web.js' const post = { "title": "Hello, world!", "commentingEnabled": true, "featured": true, "language": "en", "memberId": "8a8b9b73-4da8-47a5-8268-4396e68a0605", "richContent": { "nodes": [ { "type": "PARAGRAPH", "id": "pvirv1", "nodes": [ { "type": "TEXT", "id": "", "nodes": [], "textData": { "text": "Hello, world.", "decorations": [] } } ], "paragraphData": {} } ] } } const options = { "publish": true, // Publish a post right after creating it "fieldsets": ["URL"] // Define this fieldset to receive the URL in the payload } $w.onReady(function () { myCreateDraftPostFunction(post, options).then(() => { console.log("Created post") // create a post }) }) ``` ---