How to Create a Post via API: URL, Parameters, and Requirements?

Hi everyone,
I’m trying to create a post using an API, but I’m a bit confused about the process. Could someone guide me on how to properly structure the API request? Specifically, I’m looking for details on the correct URL, the necessary parameters, and any requirements that need to be fulfilled to successfully create a post.
Any help would be greatly appreciated!

Have you had a look at the documentation?

What specifically is unclear to you? That information will help us help you better.

2 Likes

Hey Jannis,

Thanks for replying!

I’m migrating my blog posts from WordPress to Ghost. The content from WordPress is in HTML format, but Ghost requires it in Lexical format. I’m using the Lexical library to convert the WordPress HTML content to Lexical format, but I’ve noticed that during the conversion, it’s not including span tags, anchor tags, links, URLs, etc. It’s only including <p> and <b> tags, and it’s also not preserving the styles. I have more than 6,000 posts to migrate, so I would appreciate your help with this issue.

here is my code for your reference

const { $generateNodesFromDOM } = require('@lexical/html');
const { $getRoot, $insertNodes, createEditor } = require('lexical');
const { JSDOM } = require('jsdom');

async function htmlToLexical(htmlString) {
    const editor = createEditor();
    await editor.update(() => {
        const dom = new JSDOM(htmlString);
        const document = dom.window.document;
        const nodes = $generateNodesFromDOM(editor, document);
        $getRoot().select();
        $insertNodes(nodes);
    });
    return editor.getEditorState().toJSON();
}

Thanks!

That sounds more like a question for the @lexical/html package, rather than Ghost.

But there might be an easier way to do this. Have a look here:

You probably want to look at the ?source=html parameter on the Posts API: https://ghost.org/docs/admin-api/#source-html

The code you’ve posted is not how you’d go about converting HTML to Lexical in the context of Ghost. You’ve created a completely bare-bones Lexical editor that knows nothing outside of paragraphs and basic formatting so it’s expected that you’re not preserving enough of the content semantics.

If you really want to stick to converting things yourself you can check out the @tryghost/kg-html-to-lexical package to make things easier - that’s what POST /posts/?source=html uses under the hood.

1 Like