Curl example for /v2/orders

Would someone please provide a working example of placing a new order using curl?
I have this so far, but the parameter form and formats I have used don’t work with various error messages.

curl -X POST -H “APCA-API-KEY-ID: %APCA_API_KEY_ID%” -H “APCA-API-SECRET-KEY: %APCA_API_SECRET_KEY%” https://paper-api.alpaca.markets/v2/orders?parameters -o test.out

Tried an array of params, a hash, and various strings. Can’t quite figure it out. Using Windows 10 Pro with its version of curl. Or a pointer to the API primitives rather than the API wrappers. Thanks!

curl -s --request POST -H “Content-Type: application/json” -H "APCA-API-KEY-ID: XXXXX " -H “APCA-API-SECRET-KEY: XXXXXX” -d ‘{“symbol”:“TQQQ”,“qty”:“1”,“side”:“buy”,“type”:“market”,“time_in_force”:“day”,“order_class”:“bracket”,“take_profit”: { “limit_price”: “120”},“stop_loss”:{“stop_price”:“105”}}’ “https://paper-api.alpaca.markets/v2/orders

This is a sample for a braket order

And this one is for an OCO order:

curl -s --request POST -H “Content-Type: application/json” -H “APCA-API-KEY-ID: XXXX” -H “APCA-API-SECRET-KEY: XXXXX” -d ‘{“symbol”:“TQQQ”,“qty”:“1”,“side”:“sell”,“type”:“limit”,“time_in_force”: “day”, “order_class”: “oco”, “take_profit”: {“limit_price”: “300”}, “stop_loss”: { “stop_price”: “100” }}’ “https://paper-api.alpaca.markets/v2/orders

Many thanks Mr Morales!

I spoke too soon. I put in my Sandbox keys and changed nothing else. On Debian 9, it runs without errors or output, but does nothing. Same on Windows 7 with 3rd party curl. No order was created. No complaints, no message. Checked via the API as well as the web dashboard.

On Windows 10 (using Windows version of curl) for each unaltered curl I still get
{“code”:40010000,“message”:“request body format is invalid”}

That’s the same error I have been getting all along.

Also looked at Request body format is invalid · Issue #89 · alpacahq/Alpaca-API · GitHub and tried the “working” command there on Win 10. Same result - {“code”:40010000,“message”:“request body format is invalid”}

I need to put this aside for now. Some little idiosyncracy with Windows 10 that I may never figure out.

Thanks again, Mr Morales.

Finally found time to read the documentation at curl for Windows The problem seems to be that Windows always requires double quotes as the outer quotes at the command line. To use inner double quotes you have to escape them.

For example:

curl -H “Content-Type: application/json” -H “APCA-API-KEY-ID: zzzz” -H “APCA-API-SECRET-KEY: zzzz” -d ‘{“symbol”:“AAPL”,“qty”:“1”,“side”:“buy”,“type”:“market”,“time_in_force”:“gtc”}’ “https://paper-api.alpaca.markets/v2/orders

becomes (note that POST is redundant when using -d)

curl -H “Content-Type: application/json” -H “APCA-API-KEY-ID: zzzz” -H “APCA-API-SECRET-KEY: zzzz” -d “{"symbol":"AAPL","qty":"1","side":"buy","type":"market","time_in_force":"gtc"}” “https://paper-api.alpaca.markets/v2/orders

and returns the order confirmation:

{“id”:“a1fc03cb-bfe5-4b49-a5b3-002b73363215”,“client_order_id”:“0e0bc9d6-bcee-4eb1-ad87-68ea782754ee”,“created_at”:“2022-09-13T14:35:14.314459208Z”,“updated_at”:“2022-09-13T14:35:14.314517458Z”,“submitted_at”:“2022-09-13T14:35:14.313371839Z”,“filled_at”:null,“expired_at”:null,“canceled_at”:null,“failed_at”:null,“replaced_at”:null,“replaced_by”:null,“replaces”:null,“asset_id”:“b0b6dd9d-8b9b-48a9-ba46-b9d54906e415”,“symbol”:“AAPL”,“asset_class”:“us_equity”,“notional”:null,“qty”:“1”,“filled_qty”:“0”,“filled_avg_price”:null,“order_class”:“”,“order_type”:“market”,“type”:“market”,“side”:“buy”,“time_in_force”:“gtc”,“limit_price”:null,“stop_price”:null,“status”:“pending_new”,“extended_hours”:false,“legs”:null,“trail_percent”:null,“trail_price”:null,“hwm”:null,“subtag”:null,“source”:null}

This is a Windows idiosyncrasy and has nothing to do with curl per se. If you install a different version of curl not from MSFT, my best guess is that escaping doublequotes will still be required.

D:>curl -V
curl 7.83.1 (Windows) libcurl/7.83.1 Schannel
Release-Date: 2022-05-13
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS HSTS IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI UnixSockets

and the escape character \ was stripped out of the reply.

The escape char \ is replaced by / in this example to preserve place, but the actual code should use .

For example:
curl -H “Content-Type: application/json” -H “APCA-API-KEY-ID: zzzz” -H “APCA-API-SECRET-KEY: zzzz” -d ‘{“symbol”:“AAPL”,“qty”:“1”,“side”:“buy”,“type”:“market”,“time_in_force”:“gtc”}’ “https://paper-api.alpaca.markets/v2/orders”

becomes (note that POST is redundant when using -d)

curl -H “Content-Type: application/json” -H “APCA-API-KEY-ID: zzzz” -H “APCA-API-SECRET-KEY: zzzz” -d “{/“symbol/”:/“AAPL/”,/“qty/”:/“1/”,/“side/”:/“buy/”,/“type/”:/“market/”,/“time_in_force/”:/“gtc/”}” “https://paper-api.alpaca.markets/v2/orders