Using Javascript fetch()?

Hi guys,

Rather than using the “alpacahq/alpaca-trade-api” I’d prefer to use only be using fetch. I have been unsuccessful in getting anything other than “status: 401”, " statusText: Unauthorized" as a response.

So I was wondering how I should format my fetch() request to get a proper response. Here’s a code snippet that I’ve just tried with no luck.

Granted this is a bad example, yes, but I haven’t been able to find any documentation using fetch()

 fetch("https://paper-api.alpaca.markets/v2/positions", {
		"method": "GET",
		"APCA-API-KEY-ID": "mykeyhere",
		"APCA-API-SECRET-KEY": "mysecretkeyhere"
	})

Should I fetch to something like: “https://paper-api.alpaca.markets/v2/positions?apca-api-key-id=mykey&apca-api-secret-key=mysecretkey”?

How would I correct this issue?

Seems like you might have a misconception of how http headers work, since you dont say what programming language you are using, since it looks like java look into https://github.com/mainstringargs/alpaca-java

1 Like

You are close. Changing your code to the following seems to work for me:

const positions = await fetch('https://paper-api.alpaca.markets/v2/positions', {
  'method': 'GET',
  'headers': {
    'content-type': 'application/json',
    'APCA-API-KEY-ID': API_KEY,
    'APCA-API-SECRET-KEY': API_SECRET_KEY,
  }
})
1 Like