title: magento 2 generate coupon code based on a rule programmatically
body: <p>I have a rule created in Magento 2, e.g. 10% OFF, this rule uses auto generated coupon code, instead of a pre-assigned code.</p>
  
  <p>I'd like to know how I can generate codes based on the rule programmatically.</p>
  
  <p>NOTE: This is not about creating a new rule programmatically. I want to generate codes based on a existing rule, so I can add more codes when needed but doesn't have to create a new rule every time, e.g. when a customer subscribes our mailing list, I can execute the code to generate a new code under the same rule and send it to the subscriber.</p>
  
  <p>I found this post, but this is about creating a new rule;</p>
  
  <p><a href="https://magento.stackexchange.com/questions/207311/magento2-2-programmatically-create-coupon">https://magento.stackexchange.com/questions/207311/magento2-2-programmatically-create-coupon</a></p>
  
  <p>I found the solution for Magento 1:</p>
  
  <p><a href="https://mage2-blog.com/magento-1-programmatically-create-promotion-coupon-codes/" rel="nofollow noreferrer">https://mage2-blog.com/magento-1-programmatically-create-promotion-coupon-codes/</a></p>
  
  <p>Thanks</p>