$userapi = Mage::getModel('api/user')
        ->setData(array(
        'username' => '[email protected]',
        'firstname' => 'nikhil',
        'lastname' => 'ravindran',
        'email' => '[email protected]',
        'api_key' => 'myp@ssw0rd',
        'api_key_confirmation' => 'myp@ssw0rd',
        'is_active' => 1,
        'user_roles' => '',
        'assigned_user_role' => '',
        'role_name' => '',
        'roles' => array(1) // your created custom role
        ));
    $userapi->save();
    $userapi->setRoleIds(array(1))  // your created custom role
        ->setRoleUserId($userapi->getUserId())
        ->saveRelations();

Source: https://stackoverflow.com/a/20014676