Is it possible to create a one-time use coupon that is automatically generated and emailed to a customer when they purchase a specific item?

We want to offer our customers a promotion that if they purchase item X, they get an automatic discount coupon code of Z.

Is there a way to set that up?

If not, is there an API that I can use to automate this externally?

Cheers!

Jon