Using Anon’s Messaging API, you can send and receive messages to and from Instagram for both personal and professional accounts.
To get started, you’ll need a few prerequisites:
- An API Key
- An SDK Client ID
- A JWT for one of your end users that is connected to a user pool
- An account that has been linked through the Link SDK with the JWT
Request Flow
Link
Connect your user’s Instagram account to Anon using /messenger/v2/link
List Conversations
List conversations using /messenger/v2/conversations
Send a message
Send a message using /messenger/v2/send-message
Receive a message
Register a webhook for receiving messages using /messenger/v2/webhook
Secure
If your webhook is authenticated, set your credentials using /messenger/v2/api-key
Please encourage users to enable 2FA on Instagram before connecting their accounts.
Endpoints
View our full API reference here!
URL root is
https://svc.staging.anon.com
Link an Instagram account to Anon
After your user has signed in through the Link SDK, call
/messenger/v2/link
to activate the Instagram integration. This call should be made from your backend with your secure Anon API key.
POST /messenger/v2/link
BODY
{
"userId": "the-sub-of-the-JWT",
"platform": "instagram"
}
This userId
needs to match the sub
claim in the JWT ID token used when calling /messenger/v2/link
HEADERS
Authorization: "Bearer your-jwt-id-token"
RESPONSE
{
"username": "linkedaccountusername",
"displayName": "Linked DisplayName"
}
This request may take a while (up to 30 seconds), so make sure to set an adequate timeout on your HTTP client. It is also idempotent, so if it does time out, you can safely try again without changing the account connection.
View the conversations of the connected account
List the conversations that currently exist on the connected Instagram account so that you can send a message.
This endpoint is for convenience. Typically, you’ll rely instead on the webhook we’ll register later in order to get the same information.
GET /messenger/v2/conversations?userId=your-user-id
URL PARAMS
userId=your-user-id
This userId
needs to match the sub
claim in the JWT ID token used when calling /messenger/v2/link
HEADERS
Authorization: "Bearer your-anon-api-key"
RESPONSE
[
{
"id": "conversation-id",
"participants": [
"Other InstagramUser"
]
}
]
Send a message to an Instagram conversation
Use Anon’s API to send a message to one of your user’s existing conversations.
POST /messenger/v2/send-message
BODY
{
"senderId": "your-user-id",
"conversationId": "conversation-id",
"message": "any message you would like to send"
}
This senderId needs to match the sub
claim in the JWT ID token used when calling /messenger/v2/link
HEADERS
Authorization: "Bearer your-anon-api-key"
RESPONSE
200 OK success
The response type is text
, not JSON
Register your webhook to receive incoming user messages
To receive messages on behalf of your user after linking their account, register your app’s webhook with Anon.
Anon will send a JSON payload to this webhook
every time that user receives a message, even if the user and sender have never
messaged before. Your webhook should respond with a 200
status upon successful
receipt; if it does not, Anon will retry with exponential backoff until it’s
acknowledged. This means that if your webhook has downtime, you won’t lose any
messages.
POST /messenger/v2/webhook
BODY
{
"webhook": "https://your-domain.com/path/to/webhook"
}
HEADERS
Authorization: "Bearer your-anon-api-key"
RESPONSE
200 OK success
The response type is text
, not JSON
When the connected user receives a message, you’ll receive the following payload on your registered webhook endpoint:
{
"conversationId": "conversation-id",
"message": {
"text": "Have you heard of Anon?"
},
"recipient": {
"id": "the-user-id-from-earlier",
"username": "linkedaccountusername"
},
"sender": {
"username": "someotheruser",
"displayName": "Other InstagramUser"
},
"platform": "instagram"
}
Secure your webhook
In order to ensure that only legitimate Anon traffic is accepted by your
webhook, configure it to validate an X-Api-Key
header and register that API
key with Anon. Anon will include your API key as an X-Api-Key
header.
POST /messenger/v2/api-key
BODY
{
"api-key": "secure token to authenticate webhook calls"
}
HEADERS
Authorization: "Bearer your-anon-api-key"
RESPONSE
200 OK success
The response type is text
, not JSON
Conclusion
That’s it for getting started with Anon’s Messaging API for Instagram!
To find out more, such as how to unlink
a user, check out the full
Messaging API reference.