[SOLVED] Moralis SDK Error while creating Stream

Hey guys,

I’m trying to create a stream using NGROK and I’m getting the following error:
MoralisError [Moralis SDK Core Error]: [C0006] Request failed, Bad Request(400): Could not POST to http://2405-201-a-edf6-2097-252.ngrok.io/webhooks/test. Please check your webhook URL.

This is the post route I have set up in my main index.js file:

app.post("/webhooks/test", (req, res) => {
  console.log(req.body) // Call your action on the request here
  res.status(200).end() // Responding is important
})

Any idea why this isn’t working?

Is that URL reachable with a regular POST request? You can try with a client like cURL or Postman.

Also you can return something like a message in this route handler just so it’s clearer.

I added a message but I’m not getting it. However I think I am getting some info from moralis. Here is a pic of my ngrok status page.

This is my addStream code:

const Moralis = require("moralis").default
const {EvmChain} = require("@moralisweb3/evm-utils")
// const { headers, body } = request;


async function main() {

    Moralis.start({
    apiKey: 'config.MORALIS_API_KEY',
    });


    const whoopyCreatedAbi = [{
        anonymous: false,
        inputs: [
            {
            indexed: true,
            internalType: "address",
            name: "instance",
            type: "address"
            },
            {
            indexed: true,
            internalType: "address",
            name: "creator",
            type: "address"
            }
        ],
        name: "NewClone",
        type: "event"
    }]

    const options = {  
        chains: [EvmChain.MUMBAI], // Ethereum Name Service so we only monitor Ethereum  
        description: "Created Whoopys", // your description  
        tag: "whoopyCreated", // give it a tag  
        abi: whoopyCreatedAbi,  
        topic0: ["NewClone(address,address)"],  
        includeContractLogs: true,  
        webhookUrl: "http://EDITEDOUT-.in.ngrok.io/webhook", // webhook url to receive events,  
    };

    const stream = await Moralis.Streams.add(options)

    await Moralis.Streams.addAddress({
        id: stream.id,
        address: "0x24290447ee215bb2565F53e1c6092BA8b1fhr75y"
    })
}

main() 
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

The is my index.js:

import Moralis from 'moralis';
import express from 'express';
import cors from 'cors';
import config from './config';
import { parseServer } from './parseServer';
// @ts-ignore
import ParseServer from 'parse-server';
import http from 'http';
import verifySignature from './helpers/utils'

export const app = express();

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

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(cors());

app.use(`/server`, parseServer);

app.post("/webhook", (req, res) => {
  console.log(req.body) // Call your action on the request here
  console.log("Handled!")
  res.status(200).end() // Responding is important
})

const httpServer = http.createServer(app);
httpServer.listen(config.PORT, () => {
  // eslint-disable-next-line no-console
  console.log(`Moralis Server is running on port ${config.PORT}.`);
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

You’re still getting the same Could not POST to error from the SDK? Did you try with a normal POST request to /webhook?

console.log(req.body) // Call your action on the request here
console.log(“Handled!”)

Are you getting these logged? By return a message I mean send a response back e.g.

res.send('webhook response');
res.status(200).end() // Responding is important

Added a res.send() and I’m not getting it. Even Postman requests aren’t going through.

You have to start a local server that will handle those requests, and you have to return status code 200 for those requests

Yep I started a local server and made it live using ngrok, also responding with status 200. Still not going through.

Make a simple request to that webhook url to see how it works

Here it says status code 404

I did some tweaking and now I’m getting Error 422 (unprocessable entity).
MoralisError [Moralis SDK Core Error]: [C0006] Request failed, Unprocessable Entity(422): Validation Failed {"id":{"message":"Not match in '[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-4[0-9A-Fa-f]{3}-[89ABab][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}'","value":"undefined"}}

Is this something to do with how I’m adding the stream? (I’ve posted the code above). The message says id: Not match, so it’s possibly something to do with that.

Also, in my ngrok status I’m now getting status 200, so it seems to be going through.

You can use the swagger interface directly to add a stream

This is the swagger interface:
https://api.moralis-streams.com/api-docs/

Something is missing in my url: 'https://api.moralis-streams.com/streams/evm/undefined/address',. That was the same issue in the message. Any idea why that’s coming undefined?

That should be the id of the stream that was created, where is that undefined.
After you create a stream you have to add an address to it

Hey so I just checked my Moralis Dashboard and streams were successfully added. Also, I changed my syntax from:

    await Moralis.Streams.addAddress({
        id: stream.id,
        address: "0x24290447ee215bb2565F53e1c6092BA8b1E0CC84"
    })

to:

    const {id} = stream.toJSON()
    const address = "0x24290447ee215bb2565F53e1c6092BA8b1E0CC84"

    await Moralis.Streams.addAddress({address, id})

and now the error stops showing up.

However, the streams which are added now aren’t showing up on my dashboard (I guess the one’s which are already there are one’s I created from all my testing). Do newly created streams take time to show up in the dashboard?

I would expect that all the streams will how in the dashboard, you can use the swagger interface to check the list of available streams too

try a hard reset to the browser window also

All working and uploading on time! Thank you so much! Hopefully this will help others new to creating streams!

1 Like