How to programmatically create a flag?
Check out the flag API. There are examples for creatig flags, flagging a node and more. Here the example for the flag-creation:

$flag = flag_flag::factory_by_content_type('node');

// Absolutely required, will break your site if not added properties.
$flag-&gt;name = 'my_flag_name';
$flag-&gt;title = 'My Title';

// Properties required by the UI.
$flag-&gt;roles = array(2); // An array of role IDs. 2 is for "authenticated users".
$flag-&gt;types = array('story', 'page'); // An array of node types.
$flag-&gt;flag_short = 'Flag this';
$flag-&gt;unflag_short = 'Unflag this';

// Optional properties, defaults are defined for these (and more).
// Use a print_r() or dsm() to see all the available flag properties.
$flag-&gt;global = TRUE;
$flag-&gt;flag_long = '';
$flag-&gt;flag_message = '';
$flag-&gt;show_on_form = TRUE;
$flag-&gt;show_on_node = TRUE;
$flag-&gt;show_on_teaser = TRUE;
$flag-&gt;link_type = 'toggle'; // For JS link. Other options: 'normal' and 'confirm'.

// Save the flag.
$flag-&gt;save();