Add webform field programmatically
You can alter a webform by modifying the YAML content of the config field elements:
use Drupal\webform\Entity\Webform;
use Drupal\Core\Serialization\Yaml;

  $webform = Webform::load('contact');
  $elements = Yaml::decode($webform-&gt;get('elements'));

  // add a field
  $elements['additional_field'] = [
    '#type' =&gt; 'textfield',
    '#title' =&gt; 'Additional field',
  ];

  $webform-&gt;set('elements', Yaml::encode($elements));
  $webform-&gt;save();