createResource( )
Creates a resource.
Description
The createResource()
function returns a Promise that resolves to the created resource.
Bookings resources are created with a schedule. The schedule and its sessions determine the resource's availability. Note that the schedule must contain a start date in the availability.start
property.
For resources that are available during the business's default hours, add the business's schedule as a linked schedule in the resource's schedule. For resources that have their own hours, create sessions with type "WORKING_HOURS"
using the resource's schedule.
You can use both individual and recurring sessions to define resource availability. You cannot use availability constraints for resource schedules.
When creating a resource using createResource()
, include the resource's schedule information. Set the schedule information as follows:
- If the resource uses the default business hours, get the business resource's schedule ID and include it in the
scheduleInfo.availability.linkedSchedules
array in thescheduleInfo
parameter. The default hours can bee found in the Dashboard under Settings in the Bookings section, on the Appointment hours page. - If the resource has its own custom working hours, create the resource, then create sessions of type
"WORKING_HOURS"
using thecreateSession()
function. Use thescheduleId
returned fromcreateResource()
when creating the sessions. These session can be single sessions or recurring sessions. - You can have both default business hours and custom hours for the same resource schedule by combining the steps above.
Notes:
- The Wix Bookings app does not show both default business hours and custom hours on the Staff page in the dashboard. If you've set up both custom and default business hours, only the default business hours will appear in the app, although both are working.
- The Wix Bookings app does not show non-recurring
WORKING_HOURS
sessions in the resource calendar.- You can only add the business resource's schedule as a linked schedule for a resource.
- A resource can have one schedule only.
- You can have up to 135 active resources and an additional 135 deleted resources.
- Only users with the Bookings Admin role can create a resource. You can override the role permissions by setting the
options.suppressAuth
parameter totrue
.
Syntax
function createResource(resourceInfo: ResourceInfo, scheduleInfo: Array<ResourceScheduleInfo>, [options: Options]): Promise<Resource>
createResource Parameters
NAME
TYPE
DESCRIPTION
Information to use when creating a resource.
List of schedules to be assigned to the created resource. Currently only a single schedule is allowed. If provided, any additional schedules in the resource object will be ignored.
An object representing the available options for creating a resource.
Returns
Fulfilled - Resource
Return Type:
NAME
TYPE
DESCRIPTION
Resource ID.
Resource name.
Resource email address.
Resource phone number.
Resource description.
Resource tags. Tags are used to identify, group, and filter the different types of resources. For example, 'staff' or 'room'.
List of IDs of schedules owned by this resource.
Resource status.
One of:
"CREATED"
Default status."DELETED"
The resource was deleted."UPDATED"
The resource was updated.
Was this helpful?
1import { Permissions, webMethod } from "wix-web-module";2import { resources } from "wix-bookings-backend";34export const getBusinessSchedule = webMethod(Permissions.Anyone, () => {5 return resources6 .queryResourceCatalog()7 .eq("slugs.name", "business")8 .find()9 .then((results) => {10 const businessResource = results.items[0].resource;11 return businessResource;12 });13});1415export const createStaffWithBusinessHours = webMethod(16 Permissions.Anyone,17 async (staffInfo) => {18 staffInfo = {19 name: "John Doe",20 email: "john@doe.com",21 phone: "555 4567",22 description: "Fitness Instructor",23 };2425 const businessResource = await getBusinessSchedule();26 const businessResourceScheduleId = businessResource.scheduleIds[0];27 const schedules = [28 {29 availability: {30 linkedSchedules: [31 {32 scheduleId: businessResourceScheduleId,33 },34 ],35 start: new Date(),36 },37 },38 ];39 const resource = {40 name: staffInfo.name,41 email: staffInfo.email,42 phone: staffInfo.phone,43 description: staffInfo.description,44 tags: ["staff"],45 };46 const options = { suppressAuth: true };4748 return resources49 .createResource(resource, schedules, options)50 .then((resource) => {51 return resource;52 })53 .catch((error) => {54 console.error("Failed to create resource: ", error);55 return error;56 });57 }58);5960/* Example of a returned resource:61 * "_id": "6235c808-c3c6-49f2-a000-bc8eded9f106",62 * "name": "John Doe",63 * "email": "john@doe.com",64 * "phone": "555 4567",65 * "description": "Fitness Instructor",66 * "tags": [67 * "staff"68 * ],69 * "scheduleIds": [70 * "a5d20a3a-eb25-497a-9fc1-f546ac8422fc"71 * ],72 * "status": "CREATED"73 */
Create a resource, then create 2 recurring sessions on Mondays and Wednesdays from 10 AM to 4 PM.
12import { Permissions, webMethod } from "wix-web-module";3import { resources, sessions } from "wix-bookings-backend";45export const createResourceWithRecurringSession = webMethod(Permissions.Anyone, async () => {6 let staffInfo = {7 'name': "John W. Doe",8 'email': "john@doe.com",9 'phone': "555 4567",10 'description': "Fitness Instructor"11 };121314 const resource = {15 name: staffInfo.name,16 email: staffInfo.email,17 phone: staffInfo.phone,18 description: staffInfo.description,19 tags: ['staff'],20 };2122 const resourceSchedule = [{23 availability: {24 linkedSchedules: [],25 start: new Date()26 }27 }];28 const options = { suppressAuth: true }29 let newResource;3031 try {32 newResource = await resources.createResource(resource, resourceSchedule, options)33 } catch (error) {34 console.error('Failed to create resource: ', error);35 return error;36 }3738 let recurringSession = {39 scheduleId: newResource.scheduleIds[0],40 start: {41 localDateTime: {42 year: 2021,43 monthOfYear: 3,44 dayOfMonth: 1,45 hourOfDay: 10,46 minutesOfHour: 047 }48 },49 end: {50 localDateTime: {51 year: 2021,52 monthOfYear: 3,53 dayOfMonth: 1,54 hourOfDay: 16,55 minutesOfHour: 056 }57 },58 type: 'AVAILABILITY',59 recurrence: 'FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20220101T000000Z'60 };6162 let recurrenceRules = ['FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20220101T000000Z']63 recurrenceRules.push('FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;UNTIL=20220101T000000Z')6465 let promiseList = [];66 recurrenceRules.forEach(rule => {67 try {68 recurringSession.recurrence = rule;69 console.log("recurringSession", recurringSession)70 promiseList.push(sessions.createSession(recurringSession, options))71 } catch (error) {72 console.error('Failed to create session:', error);73 }74 })7576 return Promise.all(promiseList)77 .then(sessions => {78 const resourceSessions = {79 resource: newResource,80 sessions: sessions81 };82 return resourceSessions;83 })84 .catch((error) => {85 console.error(error)86 })87});8889 /* Example returned object:90 *91 * {92 * "resource": {93 * "_id": "09ca6193-3532-4c55-bed6-5ebf8608684f",94 * "name": "John W. Doe",95 * "email": "john@doe.com",96 * "phone": "555 4567",97 * "description": "Fitness Instructor",98 * "tags": [99 * "staff"100 * ],101 * "scheduleIds": [102 * "62e49e88-c668-46a2-9033-3a0cf09013e7"103 * ],104 * "status": "CREATED"105 * },106 * "sessions": [107 * {108 * "end": {109 * "localDateTime": {110 * "year": 2021,111 * "monthOfYear": 3,112 * "dayOfMonth": 1,113 * "hourOfDay": 16,114 * "minutesOfHour": 0115 * }116 * },117 * "_id": "62e49e88c66846a290333a0cf09013e7-4eb49d5842a049fbbbc1742dd1b6ac7d",118 * "notes": "",119 * "recurrence": "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20220101T000000Z",120 * "scheduleId": "62e49e88-c668-46a2-9033-3a0cf09013e7",121 * "scheduleOwnerId": "09ca6193-3532-4c55-bed6-5ebf8608684f",122 * "start": {123 * "localDateTime": {124 * "year": 2021,125 * "monthOfYear": 3,126 * "dayOfMonth": 1,127 * "hourOfDay": 10,128 * "minutesOfHour": 0129 * }130 * },131 * "status": "CONFIRMED",132 * "tags": [],133 * "type": "WORKING_HOURS"134 * },135 * {136 * "end": {137 * "localDateTime": {138 * "year": 2021,139 * "monthOfYear": 3,140 * "dayOfMonth": 1,141 * "hourOfDay": 16,142 * "minutesOfHour": 0143 * }144 * },145 * "_id": "62e49e88c66846a290333a0cf09013e7-0f43ee04eaa6430f849b210531ce2854",146 * "notes": "",147 * "recurrence": "FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;UNTIL=20220101T000000Z",148 * "scheduleId": "62e49e88-c668-46a2-9033-3a0cf09013e7",149 * "scheduleOwnerId": "09ca6193-3532-4c55-bed6-5ebf8608684f",150 * "start": {151 * "localDateTime": {152 * "year": 2021,153 * "monthOfYear": 3,154 * "dayOfMonth": 1,155 * "hourOfDay": 10,156 * "minutesOfHour": 0157 * }158 * },159 * "status": "CONFIRMED",160 * "tags": [],161 * "type": "WORKING_HOURS"162 * }163 * ]164 */165