Drupal 7 module issues using node_ functions I'm building a basic D7 module that fetches data from an API and then add it as a certain content type. Problem is, every node_ function is coming back as undefined. node_object_prepare, node_submit, and node_save with this error message:

Call to undefined function node_object_prepare()


The module is bare bones...
info file:

name = Social Fetch
description = Fetches feeds from various social media platforms.
package = public-action
version = 1.0
core = 7.x
files[] = social_fetch.module


and the social_fetch.module...

&lt;?php
if(isset($_GET['fetch']))
{
    social_fetch();
}

function social_fetch(){
        $node = new stdClass();
        $node-&gt;title = 'test';
        $node-&gt;type = "social_boxes";
        node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
        $node-&gt;language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
        $node-&gt;uid = 1; 
        $node-&gt;status = 0; //(1 or 0): published or not
        $node-&gt;promote = 0; //(1 or 0): promoted to front page
        $node-&gt;comment = 0; // 0 = comments disabled, 1 = read only, 2 = read/write
        $node = node_submit($node); // Prepare node for saving
        node_save($node);
}


So much as I knew, node_ was part of the core and wouldn't need any dependencies but I may be wrong. I tried adding this:

module_load_include('inc', 'node', 'node.module');


but that did nothing.
also tried loading the bootstrap to see if maybe this was being run before the core loaded, but that did nothing as well.

  require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);


Am I missing something? should this be run in a hook of some kind and if so, which?

Side Note

I am able to achieve what I need by using the db_ methods below. It just seems like an inferior way to do so since I don't actually know the anatomy of a node to know if these are the only things required to make sure the system remains stable. The kind of things the node API would have handled 100%.

      $nid = db_insert('node') 
        -&gt;fields(array(
          'title' =&gt; 'test',
          'uid' =&gt; 1,
          'type' =&gt; 'social_boxes',
          'language' =&gt; 'und',
          'status' =&gt; 0,
          'comment' =&gt; 0,
          'promote' =&gt; 0,
          'created' =&gt; REQUEST_TIME,
          'changed' =&gt; REQUEST_TIME,
        ))
        -&gt;execute();

        if($nid)
        {
            db_update('node') //add vid back into the new node
              -&gt;fields(array(
                'vid' =&gt; $nid
              ))
              -&gt;condition('nid', $nid, '=')
              -&gt;execute();

            db_insert('node_revision') 
            -&gt;fields(array(
              'nid' =&gt; $nid,
              'vid' =&gt; $nid,
              'uid' =&gt; 1,
              'title' =&gt; $class-&gt;getText(),
              'timestamp' =&gt; REQUEST_TIME,
              'log' =&gt; '',
              'status' =&gt; 0,
              'comment' =&gt; 0,
              'promote' =&gt; 0,
              'sticky' =&gt; 0
            ))
            -&gt;execute();
        }
Never ever call functions from the global scope. You are probably running into a situation where other modules (the node module) are not yet loaded.

&lt;?php
if(isset($_GET['fetch']))
{
    social_fetch();
}


You could try to add your function to hook_boot(), but even then the node module might not be loaded, so you will have to ensure it is loaded manually.   drupal_load('module', 'node');

You'll end up with

function social_fetch_boot() {
  if(isset($_GET['fetch'])) {
    drupal_load('module', 'node');
    social_fetch();
  }
}