Could you please try following, written and tested with shown samples. You were close try to put your https redirection rules first in starting of your Rule file and then proceed with further.
RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(exampledomain.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^api/(d+)/?$ index.php?val=$1 [NC,L]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…