Search.../

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 the scheduleInfo 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 the createSession() function. Use the scheduleId returned from createResource() 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 to true.

Syntax

function createResource(resourceInfo: ResourceInfo, scheduleInfo: Array<ResourceScheduleInfo>, [options: Options]): Promise<Resource>

createResource Parameters

NAME
TYPE
DESCRIPTION
resourceInfo
ResourceInfo

Information to use when creating a resource.

scheduleInfo
Array<ResourceScheduleInfo>

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.

options
Optional
Options

An object representing the available options for creating a resource.

Returns

Fulfilled - Resource

Return Type:

Promise<Resource>
NAME
TYPE
DESCRIPTION
_id
string

Resource ID.

name
string

Resource name.

email
string

Resource email address.

phone
string

Resource phone number.

description
string

Resource description.

tags
Array<string>

Resource tags. Tags are used to identify, group, and filter the different types of resources. For example, 'staff' or 'room'.

scheduleIds
Array<string>

List of IDs of schedules owned by this resource.

status
string

Resource status.

One of:

  • "CREATED" Default status.
  • "DELETED" The resource was deleted.
  • "UPDATED" The resource was updated.

Was this helpful?

Create a resource using the business's hours for the resource's availability

Copy Code
1import { resources } from "wix-bookings-backend";
2
3export function getBusinessSchedule() {
4 return resources.queryResourceCatalog()
5 .eq("slugs.name", "business")
6 .find()
7 .then((results) => {
8 const businessResource = results.items[0].resource;
9 return businessResource;
10 });
11}
12
13export async function createStaffWithBusinessHours(staffInfo) {
14
15 staffInfo = {
16 'name': "John Doe",
17 'email': "john@doe.com",
18 'phone': "555 4567",
19 'description': "Fitness Instructor"
20 };
21
22 const businessResource = await getBusinessSchedule();
23 const businessResourceScheduleId = businessResource.scheduleIds[0];
24 const schedules = [{
25 availability: {
26 linkedSchedules: [{
27 scheduleId: businessResourceScheduleId
28 },],
29 start: new Date(),
30 }
31 }];
32 const resource = {
33 name: staffInfo.name,
34 email: staffInfo.email,
35 phone: staffInfo.phone,
36 description: staffInfo.description,
37 tags: ['staff'],
38 };
39 const options = { suppressAuth: true }
40
41 return resources.createResource(resource, schedules, options)
42 .then((resource) => {
43 return resource;
44 })
45 .catch((error) => {
46 console.error('Failed to create resource: ', error);
47 return error;
48 });
49}
50
51
52/* Example of a returned resource:
53 * "_id": "6235c808-c3c6-49f2-a000-bc8eded9f106",
54 * "name": "John Doe",
55 * "email": "john@doe.com",
56 * "phone": "555 4567",
57 * "description": "Fitness Instructor",
58 * "tags": [
59 * "staff"
60 * ],
61 * "scheduleIds": [
62 * "a5d20a3a-eb25-497a-9fc1-f546ac8422fc"
63 * ],
64 * "status": "CREATED"
65 */
Create a resource with its own schedule

Create a resource, then create 2 recurring sessions on Mondays and Wednesdays from 10 AM to 4 PM.

Copy Code
1import { resources, sessions } from "wix-bookings-backend";
2
3export async function createResourceWithRecurringSession() {
4 let staffInfo = {
5 'name': "John W. Doe",
6 'email': "john@doe.com",
7 'phone': "555 4567",
8 'description': "Fitness Instructor"
9 };
10
11
12 const resource = {
13 name: staffInfo.name,
14 email: staffInfo.email,
15 phone: staffInfo.phone,
16 description: staffInfo.description,
17 tags: ['staff'],
18 };
19
20 const resourceSchedule = [{
21 availability: {
22 linkedSchedules: [],
23 start: new Date()
24 }
25 }];
26 const options = { suppressAuth: true }
27 let newResource;
28
29 try {
30 newResource = await resources.createResource(resource, resourceSchedule, options)
31 } catch (error) {
32 console.error('Failed to create resource: ', error);
33 return error;
34 }
35
36 let recurringSession = {
37 scheduleId: newResource.scheduleIds[0],
38 start: {
39 localDateTime: {
40 year: 2021,
41 monthOfYear: 3,
42 dayOfMonth: 1,
43 hourOfDay: 10,
44 minutesOfHour: 0
45 }
46 },
47 end: {
48 localDateTime: {
49 year: 2021,
50 monthOfYear: 3,
51 dayOfMonth: 1,
52 hourOfDay: 16,
53 minutesOfHour: 0
54 }
55 },
56 type: 'AVAILABILITY',
57 recurrence: 'FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20220101T000000Z'
58 };
59
60 let recurrenceRules = ['FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20220101T000000Z']
61 recurrenceRules.push('FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;UNTIL=20220101T000000Z')
62
63 let promiseList = [];
64 recurrenceRules.forEach(rule => {
65 try {
66 recurringSession.recurrence = rule;
67 console.log("recurringSession", recurringSession)
68 promiseList.push(sessions.createSession(recurringSession, options))
69 } catch (error) {
70 console.error('Failed to create session:', error);
71 }
72 })
73
74 return Promise.all(promiseList)
75 .then(sessions => {
76 const resourceSessions = {
77 resource: newResource,
78 sessions: sessions
79 };
80 return resourceSessions;
81 })
82 .catch((error) => {
83 console.error(error)
84 })
85}
86
87 /* Example returned object:
88 *
89 * {
90 * "resource": {
91 * "_id": "09ca6193-3532-4c55-bed6-5ebf8608684f",
92 * "name": "John W. Doe",
93 * "email": "john@doe.com",
94 * "phone": "555 4567",
95 * "description": "Fitness Instructor",
96 * "tags": [
97 * "staff"
98 * ],
99 * "scheduleIds": [
100 * "62e49e88-c668-46a2-9033-3a0cf09013e7"
101 * ],
102 * "status": "CREATED"
103 * },
104 * "sessions": [
105 * {
106 * "end": {
107 * "localDateTime": {
108 * "year": 2021,
109 * "monthOfYear": 3,
110 * "dayOfMonth": 1,
111 * "hourOfDay": 16,
112 * "minutesOfHour": 0
113 * }
114 * },
115 * "_id": "62e49e88c66846a290333a0cf09013e7-4eb49d5842a049fbbbc1742dd1b6ac7d",
116 * "notes": "",
117 * "recurrence": "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20220101T000000Z",
118 * "scheduleId": "62e49e88-c668-46a2-9033-3a0cf09013e7",
119 * "scheduleOwnerId": "09ca6193-3532-4c55-bed6-5ebf8608684f",
120 * "start": {
121 * "localDateTime": {
122 * "year": 2021,
123 * "monthOfYear": 3,
124 * "dayOfMonth": 1,
125 * "hourOfDay": 10,
126 * "minutesOfHour": 0
127 * }
128 * },
129 * "status": "CONFIRMED",
130 * "tags": [],
131 * "type": "WORKING_HOURS"
132 * },
133 * {
134 * "end": {
135 * "localDateTime": {
136 * "year": 2021,
137 * "monthOfYear": 3,
138 * "dayOfMonth": 1,
139 * "hourOfDay": 16,
140 * "minutesOfHour": 0
141 * }
142 * },
143 * "_id": "62e49e88c66846a290333a0cf09013e7-0f43ee04eaa6430f849b210531ce2854",
144 * "notes": "",
145 * "recurrence": "FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;UNTIL=20220101T000000Z",
146 * "scheduleId": "62e49e88-c668-46a2-9033-3a0cf09013e7",
147 * "scheduleOwnerId": "09ca6193-3532-4c55-bed6-5ebf8608684f",
148 * "start": {
149 * "localDateTime": {
150 * "year": 2021,
151 * "monthOfYear": 3,
152 * "dayOfMonth": 1,
153 * "hourOfDay": 10,
154 * "minutesOfHour": 0
155 * }
156 * },
157 * "status": "CONFIRMED",
158 * "tags": [],
159 * "type": "WORKING_HOURS"
160 * }
161 * ]
162 */
163
164
165
166
167
168
169