Click on this link to read what to do if you forgot your Apple ID password --> https://support.apple.com/HT201487
If you can still sign into your account on another device you can investigate adding more ways to get a code such as multiple telephone numbers where you can receive a text or a phone call. This is based on the section "About trusted phone numbers and trusted devices" in the article: https://support.apple.com/HT204915
To see, add, or change your trusted phone numbers use one of these methods:
● On your iPhone, iPad, or iPod touch: Go to Settings > your name > Password & Security. Next to Trusted Phone Number, tap Edit.
● On a Mac computer choose Apple menu > System Settings (or System Preferences), then click your name (or Apple ID). Click Password & Security, then add or remove a trusted phone number.
● Using a web browser:
1) Go to https://appleid.apple.com and sign in.
2) Follow security options until you find one letting you change trusted numbers,
3) Click "+" to add a new number (you may be required to verify this number).
4) If you wish to remove an old number, click "-" next to the number.
Similar information for the iPhone can be found here --> https://support.apple.com/guide/iphone/manage-factor-authentication-apple-iphd709a3c46/ios
If you had set up an Account Recovery Contact ( https://support.apple.com/HT212513 ) you can also get a code that way.
If you can’t sign in, access a trusted device, reset your password, or receive verification codes, you can request account recovery to regain access to your account. This article explains how to recover your Apple ID after a waiting period - https://support.apple.com/HT204921 One tip to using account recovery it to look for options in small blue letters beneath the normal log-in items if you cannot complete the requested information. The post by shoeluvr13 at this link provides additional detail about the account recovery process: https://discussions.apple.com/thread/255401538?answerId=260044580022#260044580022
You should consider making sure you have multiple trusted devices and telephone numbers (ask a friend or a relative).
You may also wish to read the articles 'Two-factor authentication for Apple ID' ( https://support.apple.com/HT204915 ) and 'Get a verification code and sign in with two-factor authentication' ( https://support.apple.com/HT204974 ). Various sections contain information that is useful in understanding about the trusted devices (an iPhone, iPad, or Mac that Apple knows is yours) system, and how to get a verification code on a trusted device or at a telephone number.