[solved] Create Moralis Stream Error [C0008]

Hey folks,

When I try to create a Moralis Stream I get the following error:

error:  MoralisError [Moralis SDK Core Error]: [C0008] Default instance of Core is not set

Maybe important to notice: I can use other API Endpoints like “getAllStreams” or “getNFTOwners” etc., but when trying to call “getStreamHistory” or “Moralis.Streams.add” it throws the error mentioned above.

Moralis SDK Version:

    "moralis": "^2.21.0",
    "@moralisweb3/common-evm-utils": "^2.14.3",
async function getOrCreateStream(tag) {
    // Get all streams
    const allStreams = await Moralis.Streams.getAll({
        limit: 100,
    });

    // Find a stream with the tag
    let stream = allStreams.result.find(stream => stream.tag === tag);
    if (!stream) {
        stream = await Moralis.Streams.add({
            tag,
            description: "Stream",
            chains: [EvmChain.ETHEREUM],
            webhookUrl: env.MORALIS_SERVER_SYNC_WEBHOOK_URL,
            includeNativeTxs: false
        });

        await Moralis.Streams.setSettings({
            region: "eu-central-1",
        });

        console.log(stream);
    }

    if (stream.webhookUrl !== env.MORALIS_SERVER_SYNC_WEBHOOK_URL) {
        await Moralis.Streams.update({
            id: stream.id,
            webhook: env.MORALIS_SERVER_SYNC_WEBHOOK_URL,
        });
    }

    return stream;
}

async function startServer() {
    const app = express();
    app.use(bodyParser.json());

    app.post('/moralis-webhook', (req, res) => {
        console.log('Received Moralis event:', req.body);

        // Send a response back to Moralis
        res.status(200).send({ message: 'Event received' });
    });

    app.listen(env.MORALIS_SERVER_WEBHOOK_PORT, () => {
        console.log(`Moralis Webhook-Server running on port ${env.MORALIS_SERVER_WEBHOOK_PORT}`);
    });

    return app;
}

async function initMoralisStream(initStream) {
    if (!initStream) {
        return;
    }

    // Init Moralis
    await Moralis.start({
        apiKey: env.moralisApiKey,
    });


    // Start server
    await startServer();

    const stream = await getOrCreateStream(env.MORALIS_STREAM_TAG);
    console.log(stream);
}

Hi @PDev

Can you try logging env.moralisApiKey to make sure you are able to read the API key value from the env?

Hey, like I said, I can use any other API.

The call “Moralis.Streams.getAll” is working without any problem. Also tried to add a Stream manually via Admin UI, and it was returned via the API call.
And yeah, the API Key is definitely set. (Also when logging it in console)

Greets

Hi, we will test to see if there is any issue with the sdk. We will get back to you soon

2 Likes

Hi i was trying to run the same code but i was not able to get to the same error.

I noticed a typo here:

this should be webhookUrl and not webhook.

This is the code that i tried to run, which worked fine in my case

const { EvmChain } = require("@moralisweb3/common-evm-utils");
const express = require("express");
const bodyParser = require("body-parser");
const Moralis = require("moralis").default;

const env = require("dotenv").config().parsed;

async function getOrCreateStream(tag) {
  // Get all streams
  const allStreams = await Moralis.Streams.getAll({
    limit: 100,
  });

  // Find a stream with the tag
  let stream = allStreams.result.find((stream) => stream.tag === tag);
  if (!stream) {
    stream = await Moralis.Streams.add({
      tag,
      description: "Stream",
      chains: [EvmChain.ETHEREUM],
      webhookUrl: env.MORALIS_SERVER_SYNC_WEBHOOK_URL,
      includeNativeTxs: false,
      includeContractLogs: true,
    });

    await Moralis.Streams.setSettings({
      region: "eu-central-1",
    });

    console.log(stream);
  }

  if (stream.webhookUrl !== env.MORALIS_SERVER_SYNC_WEBHOOK_URL) {
    await Moralis.Streams.update({
      id: stream.id,
      webhookUrl: env.MORALIS_SERVER_SYNC_WEBHOOK_URL,
    });
  }

  return stream;
}

async function startServer() {
  const app = express();
  app.use(bodyParser.json());

  app.post("/moralis-webhook", (req, res) => {
    console.log("Received Moralis event:", req.body);

    // Send a response back to Moralis
    res.status(200).send({ message: "Event received" });
  });

  app.listen(3000, () => {
    console.log(`Moralis Webhook-Server running on port 3000`);
  });

  return app;
}

async function initMoralisStream(initStream) {
  if (!initStream) {
    return;
  }

  // Init Moralis
  await Moralis.start({
    apiKey: env.KEY,
  });

  // Start server
  await startServer();

  const stream = await getOrCreateStream(env.MORALIS_STREAM_TAG);
  console.log(stream);
}

initMoralisStream(true);

I get the right results here

node streams.js
Moralis Webhook-Server running on port 3000
EvmStream {
  _data: {
    id: '542441de-3e2e-4c1b-8609-0da68fa4da5f',
    webhookUrl: 'https://webhook.site/5be27b14-af97-4142-bb9f-f6e1cc756caa',
    description: 'Stream',
    tag: 'testing123',
    demo: false,
    topic0: undefined,
    allAddresses: false,
    includeNativeTxs: false,
    includeContractLogs: true,
    includeInternalTxs: false,
    includeAllTxLogs: false,
    getNativeBalances: [],
    triggers: [],
    abi: undefined,
    advancedOptions: undefined,
    chainIds: [ '0x1' ],
    status: 'active',
    statusMessage: 'Stream is active',
    chains: [ [EvmChain] ]
  }
}

Can you let me all the steps so i can try to reproduce it?

Will try it out later, currently not home anymore. But that could be the problem. Or is there anything I’ve to configure in the admin panel to make it work ? It should work with only the api key, right?

You are using latest version of the sdk? Yes,
It should work fine only with the api key.

1 Like

Well, thanks for the help!
I’ve actually just deleted the whole moralis packages from “node_modules” and “package.json” and re-ran “yarn add moralis”. Afterwards it worked…

Looks like there was an problem with my local installation even tho I changed nothing.

2 Likes