Where to set API key?

Hi!

I recently started a blog using a pre-configured Ubuntu 18.04 Ghost droplet at DigitalOcean. I have no web development knowledge at all but through lots of googling and trial & error, have managed to bumble my way into getting things mostly working.

One thing that’s been driving me crazy - every few minutes I get these messages in my syslog:

node[3291]: [2019-04-08 21:46:04] #033[31mERROR#033[39m
node[3291]: #033[31m
node[3291]: #033[31mThe {{#get}} helper requires your theme to have API access.#033[39m
node[3291]: #033[37m"Please enable the v2 API via your theme’s package.json file."#033[39m
node[3291]: #033[33m"See Ghost Handlebars Theme Structure - Developer Documentation "#033[39m
node[3291]: #033[1m#033[37mError ID:#033[39m#033[22m
node[3291]: #033[90m----------------------------------------#033[39m
node[3291]: #033[90mDisabledFeatureError: The {{#get}} helper requires your theme to have API access
node[3291]: at DisabledFeatureError.GhostError (/var/www/ghost/versions/2.19.1/core/server/lib/common/errors.js:10:26)

Things I have done:

  1. Installed the free Editorial theme
  2. From the Ghost admin page, created a new integration - https://myurl/ghost/api/v2/content/posts/?key=MyPublicKey appears to work
  3. Confirmed that the theme’s package.json has:
    “engines”: {
    “ghost”: “>=2.0.0”,
    “ghost-api”: “v2”
  4. Downloaded https://unpkg.com/@tryghost/content-api@1.2.0/umd/content-api.min.js into the theme’s assets/main/js directory
  5. Edited the theme’s default.hbs to contain: “<script src=”{{asset “main/js/content-api.min.js”}}“>”

I’ve googled exhaustively and read through documentation but can’t figure this out. I feel like I need to set the API key somewhere but I can’t seem to find where to set it at.

If it matters, I’m running Ghost v2.19.1.

Any help would be greatly appreciated!

Hey @thaifoon :wave: The error you are seeing is definitely a result of wrong (v1) or empty ghost-api configuration is the active theme. What you could try is activating the different theme and then reactivating the one you need again (this will repopulate api-version if you changed it by hand) or you could restart the ghost process, so it rereads theme version again. Let me know if this worked :slight_smile:

Thanks for the reply @naz !

I’ve restarted Ghost a bunch of times while troubleshooting and it hasn’t helped.

I just checked the theme’s package.json again and it definitely says ghost-api v2

@naz - I just tried your suggestion of activating a different theme (the default Casper theme) then reactivating the one I want. Unfortunately I am still getting the V2 API access error.

I also tried adding the below as a script to the code injection section:
var contentApiKey = “myapikey”;
var content_api_key = ‘myapikey’;
var site_url = “https://mywebsite.com”;

I’m basically trying anything I can manage to find via google then restarting ghost to see if it works but am pretty much out of ideas here. Help?

You Should Called Your JS After Loaded The Script

@thaifoon just tried reproducing the steps you have provided and there’s what worked for me.

  1. Started from fresh ghost installation and created an integration.
  2. Downloaded and activated Editorial theme and did not modify anything there (it’s using API v2 by default), didn’t see
  3. Added the same minified content-api SDK script from here on default.hbs template in the end of {{!-- Theme Scripts --}} section (line 62 in latest Editorial release)
  4. Added following script on default.hbs template after the script above the following example from here and the final result looks like:
    <script src="{{asset "main/js/content-api.min.js"}}"></script>

    <script>
        const api = new GhostContentAPI({
            url: 'http://ghost.local', // my local instance URL
            key: 'REDACTED',
            version: 'v2'
        });

        api.posts
            .browse({limit: 5, include: 'tags,authors'})
            .then((posts) => {
                posts.forEach((post) => {
                    console.log(post.title, post.primary_author.name);
                });
            })
            .catch((err) => {
                console.error(err);
        });
    </script>

The output of the script was as expected - fetched posts titles and primary author names. Let me know if this works for you.

A separate issue is with error messages you are receiving (The {{#get}} helper requires your theme to have API access). Can you track down the point at which they appear? Are you loading a specific page or maybe making an API call? It did not happen in my case when I used an unmodified editorial template, so wandering what could be causing it :thinking:

@naz

Thank you for that - adding your code to default.hbs did help some! I clicked around my site with the browser console open and noticed that the ‘#get helper’ error only appears on my index/home page now. The console now also shows post titles which is new and presumably good. Syslog also shows GET requests to /ghost/api/v2/content/posts/?key=myapikey&limit=5&include=tags%2Cauthors which I think is a good sign.

If I view source code of my home page in the browser, I do see two instances of "console.error("The {{#get}} helper requires your theme to have API access. Please enable the v2 API via your theme’s package.json file. See Ghost Handlebars Theme Structure - Developer Documentation “)”. The first time is after the section loading social media icons. The second is after the footer in the navigation section.

Strangely enough, that error does show up when I view source on a post’s page but not in the browser console.

I did modify the Editorial theme in an attempt to clean it up a bit and customize it to my liking so it’s very probable I did something wrong. If you can offer a clue where to start that’d be great. If not, I’ll continue my trial & error bumbling. You’ve helped quite a bit already and it’s much appreciated.

1 Like

It’s hard to tell what kind of code is causing these enable the v2 API errors without seeing them :slight_smile: To find which part of the code is causing these errors exactly would try removing the modifications you’ve done to the original theme (as original doesn’t cause errors) and then would add these modifications step by step while checking for errors.

Would love to know what caused this error in the end, maybe would save a headache somebody else trying similar thing :wink:

@naz

Figured it out! In an attempt to be non-destructive I was commenting out sections I did not want, like this part from sidebar.hbs:

<!--        {{!-- Contact --}}
    {{#get "posts" slug="contact"as |contact|}}
        {{#if contact}}{{#foreach contact}}
            <section>
                <header class="major">
                    <h2>{{title}}</h2>
                </header>
                {{content}}
            </section>
        {{/foreach}}{{/if}}
    {{/get}}  -->

Or in the case of my index.hbs, I did not comment this out:

{{!-- Featured Post --}}
{{#is "home"}}
{{#get "posts" filter="featured:true" limit="1" as |featured_post|}}
{{#foreach featured_post}}
    <article id="banner">
        <div class="content">
            <header>
                <h1><a href="{{url}}">{{title}}</a></h1>
                {{#if custom_excerpt}}<p>{{custom_excerpt}}</p>{{/if}}
            </header>
            {{content words="42"}}
            <ul class="actions">
                <li><a href="{{url}}" class="button big">Learn More</a></li>
            </ul>
        </div>
        {{#if feature_image}}
        <a href="{{url}}" class="image object">
            <img src="{{img_url feature_image}}" alt="{{title}}" />
        </a>
        {{/if}}
    </article>
{{/foreach}}

{{/get}}
{{/is}}

I don’t understand why commenting out the sections still caused me to see the script errors when I viewed source in the browser but totally deleting the ‘featured post’ section from index.hbs fixed the error messages spamming my syslog. Not sure what modifications I made would have caused API errors there but I’m just happy things are working better for now.

Thank you again for the help!

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