How to update a post using Admin API in Javascript?

I want to use Admin API from @tryghost/admin-api to update the contents of a post.

As noted in AdminAPI: getting UPDATE_COLLISION · Issue #103 · TryGhost/SDK · GitHub the problem which I have is an API misuse. Can somebody explain how to use Admin API to update the contents of a post?

This was my attempt:

await api.posts.edit({"id": "5c8051da2a25b600c01ff9ee", "mobiledoc": JSON.stringify(md), "updated_at": "2019-05-09T11:39:37.000Z"})

Fails with:

{ UpdateCollisionError: Saving failed! Someone else is editing this post.
at makeRequest.catch (/tmp/node_modules/@tryghost/admin-api/lib/index.js:292:33)
at process._tickCallback (internal/process/next_tick.js:68:7)

name: ‘UpdateCollisionError’,
context: ‘Saving failed! Someone else is editing this post.’,
type: ‘UpdateCollisionError’,
details:
{ clientUpdatedAt: ‘2019-05-09T11:39:37.000Z’,
serverUpdatedAt: ‘2019-05-08T15:40:57.000Z’ },
property: null,
help: null,
code: ‘UPDATE_COLLISION’,
id: ‘36069d20-723f-11e9-9b05-d55f47be1c26’ }

Nobody else is editing that post in reality.

This was answered already here Add More API End Points - #11 by Kevin

Thanks, this helped. I hope it will be included into the docs.

It is in the docs here Ghost Admin API Documentation

The updated_at field is required as it is used to handle collision detection, and ensure you’re not overwriting more recent updates. It is recommended to perform a GET request to fetch the latest data before updating a post.

If you have any suggestions for improvements you can always open PRs - the docs are open source.

I mean the JS library API docs, not the raw REST of course.

The JS library API is a very thin wrapper around the REST API and will only be documented as such - the docs for the JS library even point you to the REST API docs for usage.

All endpoints & parameters provided to integrations by the Admin API are supported.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.