Call to a member function code() on null

I’m trying to implement lord-executor’s Kirby JSON API and call this through Javascript. The documentation recommends to use a snippet to expose some of the Kirby settings.

On the line:
<?php echo $site->language()->code(); ?>

I get the error:
“Call to a member function code() on null”

How can I resolve this?

Does the language object exist?

dump($site->language());

I believe not, nothing appears. I thought it took the global default. How can I create one?

Are you on a multi-language site at all? If not, the language object is not available and does not make sense.

Ok, I just thought it was required for this snippet to do the AJAX call.

<script type="text/javascript">
        /**
         * Kirby JavaScript settings object. It contains settings and functions that JavaScript code
         * potentially needs to know about when dealing with Kirby
         */
        window.Kirby = {
            lang: '<?php echo $site->language()->code(); ?>',
            apiPrefix: '<?php echo c::get('jsonapi.prefix', 'api'); ?>',
            baseUrl: '<?php echo $site->url(); ?>',
            langUrl: '<?php echo $site->language()->url(); ?>',
            url: function (path) {
                return this.baseUrl + (path && path[0] === '/' ? path : '/' + path);
            },
            pageUrl: function (path) {
                return this.langUrl + (path && path[0] === '/' ? path : '/' + path);
            },
            apiUrl: function (path) {
                var url = this.baseUrl + '/' + this.apiPrefix + (path && path[0] === '/' ? path : '/' + path);
                url += (url.indexOf('?') < 0 ? '?' : '&') + 'lang=' + this.lang;
                return url;
            },
        };
    </script>

No, I think that is just an example for a multi-language site (haven’t used the plugin yet, though)

Apologies, for the edit. Thank you