When you took the job and accepted the sign-on bonus, you also agreed that you either would stay for 2 years or pay back the bonus. Assuming it is an appropriately worded term in a valid contract, you made an enforceable promise.
Your employer would be within its rights to take legal action to pursue repayment of your bonus. However, depending on the size of the bonus, it that might be too expensive. Of course, this does not mean your employer won’t pursue repayment, because it may have other motivations. For example, the employer might choose to enforce the term as an example to other employees.
More importantly, if you keep this bonus despite your promise to pay it back if you left within two years, you will be burning bridges with this employer.
If you sit down and talk to your employer, you may be able to negotiate something. I’m sure they don’t want you to stay – unhappy and resentful – solely because of your sign-on bonus.
You can read more about employee rights at
https://www.workingnowandthen.com/.
This response is not legal advice, but is general information only, based upon the information stated in the question and general legal principles. It is provided for general educational purposes of the public who may have similar questions, not for any specific individual or circumstance. It is not intended to create an attorney-client relationship. Legal issues depend on all the specific facts of a situation, which are not present here. If you would like to obtain specific legal advice about your issue, you must contact a local attorney who is licensed to practice law in your state.