API authentication for create alerts
Asked by beta.lam:

I am new to Opsgenie, trying to create alerts through calling Rest API in my own java application

Problem:

The request simply included the parameters "message" and "alias", but get an return of error code 40301

Checked / Tried:

1. The API key used is new created. Can be found under Teams =\> Integrations, in the Opsgenie web page. Also the team is a new team.

2. Can get the list and details of existing alerts through Alert APIs using the same API Key. The existing alerts are created through Opsgenie web page, selected the same integration in "API integration" field

3. Checked the API integration is "true" in "allowConfigurationAccess", "allowDeleteAccess", "allowReadAccess", and "allowWriteAccess" through Integration APIs

4. Tried changing the path to [https://api.eu.opsgenie.com](https://api.eu.opsgenie.com/ "") in case I am using EU instance. It returns "Could not authenticate" so I don't think I am using the EU instance.

May someone kindly tell what is missing in my case?

Update:  
Checked on the header "Authorization" and payload.  
Here is my testing curl command:

```
curl -X POST https://api.opsgenie.com/v2/alerts -H "Content-Type: application/json" -H "Authorization: GenieKey MyApiKey" -d '{"message": "An example alert message", "alias": "Life is too short for no alias", "description":"Every alert needs a description", "responders":[{"id":"MyTeamId","type":"team"}],"visibleTo":[{"id":"MyTeamId","type":"team"}],"priority":"P4"}'
```

And the response I got

```
{"code":40301,"message":"To perform this action, use an API key from an API integration.","took":0.009,"requestId":"7f74b148-94b5-4352-be0f-a50ed381a842"}
```


Answers:
author: Jonathan Green
body: If you are sending to create alerts

You want to POST @ **<https://api.eu.opsgenie.com/v2/alerts>**

As a header you want to make sure you have:  
"**Authorization** ": "**GenieKey yourapikeyorvariablesubstituted** "  
OR

"**Authorization** ": "**GenieKey ${APIKEY}**"

Make sure your payload is right:

JSON example:  
**{**

**"message": "My Alert",**

**"description": "My Amazing Description",**

**"details": {**

**"YourKey1": "YourValue1",**

**"YourKey2": "YourValue2",**

**"ETC": "ETC"**

**}**

**}**   

You are going to have to break out everything nested in advance and map like the above. I've never been able to get anything else to work. There seems to be a fail-all instead of any sanitization/stringify within opsgenie's api.

Hope this helps EU friends!

comments: author: beta.lam
body: I have checked on the points you mentioned and I think I my request is valid.  
Updated my question to include my request and the response I get.


author: Jonathan Green
body: Try removing the message altogether and just focus on the GET to the same endpoint. Can you get a valid response?

If not, I would check your stored variable(MYAPIKEY) for any ' or " and remove them. Just guessing, its a charactering that is breaking the JSON structure at the api key.   

The following works for me -\>   
curl -X GET "<https://api.eu.opsgenie.com/v2/alerts>" -H "Authorization: GenieKey $MYAPIKEY"  

Variable is Stored as MYAPIKEY=1234567890  

If you get this far, switch your request to POST, add -H "Content-Type: application/json" and your body with { "message": "Test Message", "description": "Test Description" }

If your still getting the green light, add in your normal messages and descriptions from your workflow and continue down one-by-one until you've got the exact message you desire.


author: beta.lam
body: That's the point no. 2 of what I have tried. Can get the list and details of existing alerts through GET request. But it gives error response when I want to create alert through POST request.

```
curl -X GET https://api.opsgenie.com/v2/alerts -H "Authorization: GenieKey MyApiKey"


{"data":[{"seen":true,"id":"RandomId","tinyId":"1","alias":"MyAlias","message":"This is a test alert. Manually typed.","status":"closed","acknowledged":true,"isSeen":true,"tags":[],"snoozed":false,"count":1,"lastOccurredAt":"2020-09-03T08:56:39.322Z","createdAt":"2020-09-03T08:56:39.322Z","updatedAt":"2020-09-03T09:16:47.838Z","source":"teammate@myteam.com","owner":"teammate@myteam.com","priority":"P1","teams":[{"id":"MyTeamId"}],"responders":[{"type":"team","id":"MyTeamId"}],"integration":{"id":"MyApiIntegrationId","name":"MyApiName","type":"API"},"report":{"ackTime":1205489,"closeTime":1208516,"acknowledgedBy":"teammate@myteam.com","closedBy":"teammate@myteam.com"},"ownerTeamId":"MyTeamId"}],"paging":{"next":"https://api.opsgenie.com/v2/alerts?limit=20&sort=createdAt&offset=20&order=desc","first":"https://api.opsgenie.com/v2/alerts?limit=20&sort=createdAt&offset=0&order=desc","last":"https://api.opsgenie.com/v2/alerts?limit=20&sort=createdAt&offset=20&order=desc"},"took":0.044,"requestId":"774310b3-9dcd-451c-9fe6-ccbe2d2a20ff"}
```

```
curl -X POST https://api.opsgenie.com/v2/alerts -H "Content-Type: application/json" -H "Authorization: GenieKey MyApiKey" -d '{"message": "Test Message", "description": "Test Description" }'

{"code":40301,"message":"To perform this action, use an API key from an API integration.","took":0.01,"requestId":"fdee5a42-9162-4474-93ed-95198ca99065"}
```


author: Jonathan Green
body: To clarify, should you be using the EU API subdomain?  
<https://api.eu.opsgenie.com>


author: beta.lam
body: No. Since I do not know which instance I am actually using, I tried on both normal and EU API (as stated in pt.4 in the tried action list).  
And I got "Could not authenticate" response when using EU subdomain. So I don't think I should use EU API subdomain.

Trials in using EU subdomain:

```
curl -X GET -H "Authorization: GenieKey MyApiKey" https://api.eu.opsgenie.com/v2/alerts

{"message":"Could not authenticate","took":0.0,"requestId":"1ef45d44-cc3d-4e8c-bcc0-6aab152df4f1"}
```

```
curl -X POST https://api.eu.opsgenie.com/v2/alerts -H "Content-Type: application/json" -H "Authorization: GenieKey MyApiKey" -d '{"message": "Test Message", "description": "Test Description" }'

{"message":"Could not authenticate","took":0.007,"requestId":"781506fd-22fb-467d-b783-de275009ba77"}?
```


author: Nick H
body: Hi [@beta.lam](/t5/user/viewprofilepage/user-id/4080921) ,

If your account is an EU instance, the account URL would be something like <https://accountname.app>.**eu** .opsgenie.com/ - otherwise US instances have the URL as [https://accountname.app.opsgenie.com](https://wrathion.app.opsgenie.com).


author: Nick H
body: Also like one of those errors indicates:

```
{"code":40301,"message":"To perform this action, use an API key from an API integration.","took":0.01,"requestId":"fdee5a42-9162-4474-93ed-95198ca99065"}
```

The Alert API (specifically the Create Alert API request) requires the API key to be from an API Integration. We recommend using a global API integration - meaning it is assigned to \[No Team\], and has these permissions:  
![apicomm.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/110644i6163BBC22405A016/image-size/large?v=v2&px=999 "apicomm.jpg")


author: Nick H
body: And since it look like your request was successful with a GET request using

```
https://api.opsgenie.com/v2/alerts
```

your account is a US instance.


author: Ricardo Felgueiras
body: Hi, all.

I'm having the exact same problem.

Is this problem solved?

Thanks in advance.


author: Nick H
body: Hi [@Ricardo Felgueiras](/t5/user/viewprofilepage/user-id/4157224)

I provided an answer to your other Community post which seems related to this issue:

[https://community.atlassian.com/t5/Opsgenie-questions/OpsGenie-API-Create-alert-Authentication-problem/qaq-p/1531047?utm_source=atlcomm\&utm_medium=email\&utm_campaign=immediate_general_question\&utm_content=topic#U1531256](https://community.atlassian.com/t5/Opsgenie-questions/OpsGenie-API-Create-alert-Authentication-problem/qaq-p/1531047?utm_source=atlcomm&utm_medium=email&utm_campaign=immediate_general_question&utm_content=topic#U1531256)

Let us know if you have additional questions or issues.


author: Ricardo Felgueiras
body: Thank you.


Tags: api

cloud