Cloning nodes between multisite instances with separate databases I'm working on a Drupal 8.6 multisite and my client wants to be able to duplicate content between many of these sites. The codebase of the websites is the same, but each of them uses a different database. For that, I made a module with some hooks that gets the node serialized and posts it to another site. code below:

function a8_replicate_node_insert(Node $node) {
  $type = $node-&gt;getType();

  if($type == 'event')
  {
    $portals = $node-&gt;get('field_duplicate_to_portal')-&gt;getValue();
    $serializer = \Drupal::service('serializer');
    $data = $serializer-&gt;serialize($node, 'json', ['plugin_id' =&gt; 'entity']);
    $data = json_decode($data); // make it array again

    foreach ($portals as $url) {
      try{
        a8_replicate_post_to_other_site($url['value'], $data, 'insert');
      }
      catch (\Exception $e){
        die($e-&gt;getMessage());
      }
    }

  }
}


The a8_replicate_post_to_other_site function just uses curl to post the data as json to the other website. Then I made another module to receive the content, with the following function:

  public function receive() {
    $ip = \Drupal::request()-&gt;getClientIp();
    // to make sure it's the machine posting to itself
    if($ip == '127.0.0.1') {
      $data = json_decode(file_get_contents('php://input'), true);

      if($data['action']=='insert') {

        $node = Node::create($data['node']);
        $node-&gt;save();// &lt;&lt;&lt;&lt; error happens here 
      }
    }
  }


It's my first Drupal development and I'm I don't know pretty well what I'm doing.

Is this the right way to create a node? Is there a better and safer way to do it?
I wouldn't use the approach you used. Here is my recommendation. 

First, define your database connection to each multisite on the default setting.php with different name, check the example below: 

#Default site 

$databases['default']['default'] = array (
  'database' =&gt; 'dbname',
  'username' =&gt; 'uname',
  'password' =&gt; 'password',
  'prefix' =&gt; '',
  'host' =&gt; 'host',
  'port' =&gt; '3306',
  'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' =&gt; 'mysql',
);

#Site 1
$databases['site1']['default'] = array (
  'database' =&gt; 'dbname',
  'username' =&gt; 'uname',
  'password' =&gt; 'password',
  'prefix' =&gt; '',
  'host' =&gt; 'host',
  'port' =&gt; '3306',
  'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' =&gt; 'mysql',
);

#Site 2
$databases['site2']['default'] = array (
  'database' =&gt; 'dbname',
  'username' =&gt; 'uname',
  'password' =&gt; 'password',
  'prefix' =&gt; '',
  'host' =&gt; 'host',
  'port' =&gt; '3306',
  'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' =&gt; 'mysql',
);




Now, on hook_node_insert you can save the node object to multiple databases by changing the database each time, refer the code below as an example:

// Switch to site 1 database 
\Drupal\Core\Database\Database::setActiveConnection('site1');
//$node will have all fields you need 
$site1Node = Node::create([
    'type'        =&gt; $node-&gt;getType();,
    'title'       =&gt; $node-&gt;getTitle();,
    'body_text'   =&gt; $node-&gt;get('body')-&gt;value,
]);
$site1Node-&gt;save();
// Switch to site 2 database 
\Drupal\Core\Database\Database::setActiveConnection('site2');

$site2Node = Node::create([
    'type'        =&gt; $node-&gt;getType();,
    'title'       =&gt; $node-&gt;getTitle();,
    'body_text'   =&gt; $node-&gt;get('body')-&gt;value,
]);
$site2Node-&gt;save();

// finnally make sure to swich back to default datyabase 
\Drupal\Core\Database\Database::setActiveConnection();