How to update a post using Admin API in Javascript?

#1

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

As noted in https://github.com/TryGhost/Ghost-SDK/issues/103 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.

#2

This was answered already here Add More API End Points

#3

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

#4

It is in the docs here https://docs.ghost.org/api/admin/#updating-a-post

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.

#5

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

#6

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.

closed #7

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