[SOLVED] Streams API get Error : Default instance of MoralisCore is not set

I got email Live Streams API today and so happy to implement new this feature.I follow sample code on Moralis Doc but got error :

{"name":"Moralis SDK Core Error","source":"server","message":"[C0008] Default instance of MoralisCore is not set","stack":"Moralis SDK Core Error: [C0008] Default instance of MoralisCore is not set\\n    at Function.MoralisCoreProvider.getDefault }

/api/addStream.js

My code :

import Moralis from 'moralis';
import { EvmChain } from "@moralisweb3/evm-utils";

Moralis.start({
  apiKey: process.env.MORALIS_API_KEY,
});

const stream = {
  chains: [EvmChain.GOERLI], // list of blockchains to monitor
  description: "monitor Bobs wallet", // your description
  tag: "bob", // give it a tag
  webhookUrl: "mystite.com", // webhook url to receive events,
}

How to fix issues? thanks for reading

Which documentation page are you following? Which version of moralis are you using? What sort of app are you using e.g. Express, Next.js?

Link doc:

Use Moralis v1
Using Nextjs

What is the exact version you’re using? It needs to be moralis e.g. 2.6.7, not moralis-v1 if that’s what you’re using.

“moralis”: “^1.11.0”,
@moralisweb3/core”: “^2.6.7”,
@moralisweb3/evm-api”: “^2.6.7”,
@moralisweb3/evm-utils”: “^2.6.7”,

You should be using moralis@2x. You can install the latest with npm install moralis or yarn add moralis.

I update to moralis@2x and mysite down with error :

error - TypeError: Cannot read properties of undefined (reading ‘getTokenBalances’)

react-moralis": “^1.4.0”.

I must update react-moralis ?

In that case, as you are also using moralis-v1, you need to use these versions:

[email protected] 
[email protected]

The documentation you linked is for the 2.0 Node.js SDK which is a separate library - so if you want to use that in your Next.js app as well - you need to use [email protected].

So at the end you should have:

[email protected] 
[email protected]

[email protected]
// along with the rest - evm-api/evm-utils

I changed
image
image

My site comeback :slight_smile: but still got same error Stream API:

{"name":"Moralis SDK Core Error","source":"server","message":"[C0006] Request failed}

It looks like you were trying to use Next.js API, what is your full Next.js API code including your handler?

Yes, I use NextJS


My code :

import md5 from 'md5';
import Moralis from 'moralis';
import {EvmChain} from '@moralisweb3/evm-utils';

export default async function handler(req, res) {
  const body = req.body;
  let token = req.headers.token;

  const hash = md5(JSON.stringify(body) + '|' + process.env.NEXT_PUBLIC_API_KEY);

  if (!token && token !== hash) {
    return res.status(401).json({error: 0, message: 'You are unauthorized to make this request'});
  }

  switch (req.method) {
    case 'POST':
      Moralis.start({
        apiKey: process.env.NEXT_PUBLIC_MORALIS_STREAM_API_KEY,
      });

      const stream = {
        chains: [EvmChain.GOERLI], // list of blockchains to monitor
        description: 'monitor custom lofi wallet', // your description
        tag: 'lofi', // give it a tag
        webhookUrl: process.env.NEXT_PUBLIC_URL + '/api/webhook', // webhook url to receive events,
      };

      const newStream = await Moralis.Streams.add(stream);
      const {id} = newStream.toJSON(); // { id: 'YOUR_STREAM_ID', ...newStream }

      // Now we attach bobs address to the stream
      const address = body.address;
      const result = await Moralis.Streams.addAddress({address, id});

      console.log('body', result);
      res.status(200).json({error: 1, message: 'Successfully'});

      break;
    case 'GET':
      res.status(405).send({message: 'Only POST requests allowed'});
  }
}

Hi bro, I got it. Worked now :).

I got error:
image

And I change code:

Need param includeNativeTxs . You missing in sample code

Thanks for support Glad

Thanks that has been added in that example.

2 Likes

I’m not seeing what solved the original Default instance . . . issue.

I’ve tried it in a NextJS environment as well as a newly minted npm init setup and I still receive Default instance of MoralisCore is not set.

Attempting the example EXACTLY as posted here I end up with Moralis.start is not a function

:thinking:

Hey @coder,

can you create a new thread in forum and show how does your code looks like?

Will be closing this thread as this is a separate solved thread.

Thanks!

1 Like