DatoCMS from PHP Guzzle

Hi
I am trying to use Content management API from PHP with Guzzle. I can easily get the list of records, but while creating new records I am getting issue. Code-

$headers = [
                'X-Api-Version' => '3',
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer '.$this->apiToken,
                    
            ];
            $params = [
                'data' => [
                    'type' => 'marketad',
                    'attributes' => [
                        'site' => 58096371,
                        'section' => 58096144,
                        'title' => 'Test from shop api',
                        'folder' => 58096601,
                        'sub_folder' => 58118224
                    ],
                    'meta' => [
                        'created_at' => '',
                        'first_published_at' => ''
                    ],
                    'relationships' => [
                        'item_type' => [
                            'data' => [
                                'type' => 'item_type',
                                'id' => '1206971'
                            ]
                        ]
                    ]
                ]
            ];

            $datoCmsResponse = $this->client->request(
                'POST',
                'https://site-api.datocms.com/items',
                [
                    'headers' => $headers,
                    'json' => $params,
                ]
            );

Response-

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 4d120e
                    [type] => api_error
                    [attributes] => Array
                        (
                            [code] => INVALID_FORMAT
                            [details] => Array
                                (
                                    [messages] => Array
                                        (
                                            [0] => #/data/type: failed schema #/definitions/item/links/3/schema/properties/data/properties/type: marketad does not match /^item$/.
                                        )

                                )

                        )

                )

        )

)

In the model-

  • site, section, folder and sub_folder are single link referenced to another models
  • title is single line string

Need some help here, because the error is not clear to me. Also is there any list of errors with details?

Hello @tarek the error is “failed schema”, which I think means that you are not matching the schema of the record.

For now, when creating a record you need to pass all the fields, even if they are not required.

I know it’s counterintuitive and we are going to change this soon, but it’s like this for now. So if you have a field which is empty pass in a null value.

Hope this helps!

Hi @mat_jack1 tried with all the fields, but still the same. So I created a test_model with title field only. Still getting same error with-

$params = [
    'data' => [
        'type' => 'test_model',
        'attributes' => [
            'title' => [
                'no' => 'Test from shop api'
            ]
        ],
        'meta' => [
            'created_at' => '',
            'first_published_at' => ''
        ],
        'relationships' => [
            'item_type' => [
                'data' => [
                    'type' => 'item_type',
                    'id' => '1330794'
                ]
            ]
        ]
    ]
];

@mat_jack1 found the issue. Don’t bother to check.

1 Like