[#1233] Cannot create booking ,booking API

Migrated from Redmine #1233 | Author: edward sh
Status: Feedback | Priority: High, I’m very impatient | Created: 2024-05-17


Hi,
I’m encountering an issue with the booking API implementation in Node.js and need your assistance. Below is the code I’m currently working with:


async function createBooking(serviceId,providerId,date,time) {
    try {
        const token = await getToken(); 
        const headers = {
            'Content-Type': 'application/json',
            'X-Company-Login': companyLogin,
            'X-Token': token
        };
        
        const requestBody = {
            start_datetime: `${date} ${time}`,
            provider_id:providerId,
            service_id:serviceId,
            client_id:46,

        }
        
        const response = await axios.post("https://user-api-v2.simplybook.me/admin/bookings", requestBody, { headers });

        return response.data;
    } catch (error) {
        console.error('Error making booking:', error);
        throw error;
    }
}

The error message I’m receiving is as follows:
Request failed with status code 500
I’ve been following the guidelines provided,API documentation | SimplyBook.me Online Scheduling but it seems that I might be missing something or following an incorrect procedure. Could you please review my code and guide me on what might be going wrong?

could you provide an example code for implementing a booking feature? That would be incredibly helpful.

Thank you in advance for your support.

Dmytro Bondarev wrote:

Hi,

Please provide HTTP request and response.

edward sh wrote:

i’m working locally so my response is:

{
    "success": false,
    "error": {
        "message": "Request failed with status code 400",
        "name": "AxiosError",
        "stack": "AxiosError: Request failed with status code 400\n    at settle (C:\\Eatsham Projects\\MyPrivateChemist\\mpc_server\\node_modules\\axios\\dist\\node\\axios.cjs:1966:12)\n    at IncomingMessage.handleStreamEnd (C:\\Eatsham Projects\\MyPrivateChemist\\mpc_server\\node_modules\\axios\\dist\\node\\axios.cjs:3065:11)\n    at IncomingMessage.emit (node:events:529:35)\n    at endReadableNT (node:internal/streams/readable:1400:12)\n    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)\n    at Axios.request (C:\\Eatsham Projects\\MyPrivateChemist\\mpc_server\\node_modules\\axios\\dist\\node\\axios.cjs:3876:41)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async Object.createBooking (C:\\Eatsham Projects\\MyPrivateChemist\\mpc_server\\services\\Simplybook.js:223:26)\n    at async testMethod (C:\\Eatsham Projects\\MyPrivateChemist\\mpc_server\\controllers\\appointController.js:244:22)",
        "config": {
            "transitional": {
                "silentJSONParsing": true,
                "forcedJSONParsing": true,
                "clarifyTimeoutError": false
            },
            "adapter": [
                "xhr",
                "http"
            ],
            "transformRequest": [
                null
            ],
            "transformResponse": [
                null
            ],
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1,
            "maxBodyLength": -1,
            "env": {},
            "headers": {
                "Accept": "application/json, text/plain, */*",
                "Content-Type": "application/json",
                "X-Company-Login": "privatechemist",
                "X-Token": "3d1f963afc90da7f960cbcffdb4935c33fea32bd2255e138e33e6116b96c7c11",
                "User-Agent": "axios/1.6.8",
                "Content-Length": "55",
                "Accept-Encoding": "gzip, compress, deflate, br"
            },
            "method": "post",
            "url": "https://user-api-v2.simplybook.me/admin/bookings",
            "data": "{\"start_datetime\":\"undefined undefined\",\"client_id\":46}"
        },
        "code": "ERR_BAD_REQUEST",
        "status": 400
    }
}

which http request you are looking for

Dmytro Bondarev wrote:

Sorry it is not HTTP request/response.
But as you can see our server returns 400 error. You can see details why in response.
You should handle this error correctly on your side.

edward sh wrote:

could you please provide me an example code for implementing a booking feature with the required body request and correct format so it will be easy for me to understand the api structure

Dmytro Bondarev wrote:

Hi you can see example in our documentation:

You code looks correct, but it seems that some parameters are not valid. You should handle errors correctly on your side.

edward sh wrote:

after creating booking i got response of booking where invoice is null, so how invoice will create and client can pay the invoice

Dmytro Bondarev wrote:

This admin API and you can only make delay payment for this please see documentation and pass relevant parameters

accept_payment: true,
payment_processor: ‘delay’

Then you will be able to receive payment link API documentation | SimplyBook.me Online Scheduling

edward sh wrote:

What im missing here:


async function createBook(start_datetime,end_datetime,providerId,serviceId,clientId,categoryId,paymentProcessor) {
    const url = 'https://user-api-v2.simplybook.me/admin/bookings';
    const token = await getToken();
    const headers = {
      'Content-Type': 'application/json',
      'X-Company-Login': companyLogin,
      'X-Token': token
    };
  
    const bookingData = {
      count: 1,
      start_datetime: start_datetime,
      end_datetime: end_datetime,
      provider_id: providerId,
      service_id: serviceId,
      client_id: clientId,
      category_id: categoryId, 
      additional_fields: [
        {
          field: '623ab80dded6c4ee4f79a9a9636e2097',
          value: 'Male'
        }
      ],
      accept_payment: true,
      payment_processor: paymentProcessor
    };
  
    try {
      const response = await axios.post(url, bookingData, { headers });
      console.log('Response received:', response.data);
  
      return response.data;
    } catch (error) {

      console.error('Error :', error.message);
      throw error;
    }
}

i got error: Request failed with status code 400
Error data: {
code: 400,
message: ‘’,
data: { additional_fields: [ [Object], [Object], [Object] ] },
message_data:
}
Error status: 400

edward sh wrote:

im following rest api : API documentation | SimplyBook.me Online Scheduling

Dmytro Bondarev wrote:

You haven’t passed some required fields. Or you passed it in wrong format.
You can see it in response from server.

For more details please provide http request and response.

edward sh wrote:

I pass every field except one “location_id” in my code because I don’t have one.
As I asked you before, if you could please provide me an example code with the required field and format, it would be very helpful. and currently, I’m working locally so I don’t know which HTTP request and response you want.

Dmytro Bondarev wrote:

I already sent you example.
Collection of intake forms depends on your settings. In the data that you provided it is obvious that some intake forms are not valid. I told you about that.
What is exactly wrong you can see in response from server.

You are working locally or not locally (whatever it means) you are making http request to our server and our server returns you response. This request and response required to tell you exactly what is wrong.

edward sh wrote:

I got it, one additional field was missing, but at least the server response should tell what is missing or required, but no such information, so please fix it, I’m struggling for 3 days.

Dmytro Bondarev wrote:

Server returns this information, that is why I asked for server response.