Skip to content

Instantly share code, notes, and snippets.

@Anonyfox
Created August 12, 2023 09:26
Show Gist options
  • Save Anonyfox/3f36c26fb00a9bc4067d6dc9c9a87137 to your computer and use it in GitHub Desktop.
Save Anonyfox/3f36c26fb00a9bc4067d6dc9c9a87137 to your computer and use it in GitHub Desktop.
Call Salesforce Rest API with Elixir
# inspiration/guide: https://developer.sage.com/api/financials/how-to/configure-authentication/
defmodule Salesforce do
@user_email "XXX"
@user_password "XXX"
@client_id "XXX"
@client_secret "XXX"
def get_credentials() do
response = HTTPoison.post!(
"https://login.salesforce.com/services/oauth2/token",
{:form, [grant_type: "password", client_id: @client_id, client_secret: @client_secret, username: @user_email, password: @user_password]},
[{:"Content-Type", "application/x-www-form-urlencoded"}]
)
|> Map.get(:body)
|> Jason.decode!()
%{access_token: response["access_token"], instance_url: response["instance_url"]}
end
def query(credentials, soql) do
url = "#{credentials.instance_url}/services/data/v43.0/queryAll?#{URI.encode_query(q: soql)}"
HTTPoison.get!(url, [{:Authorization, "Bearer #{credentials.access_token}"}, {:"Content-Type", "application/json"}])
|> Map.get(:body)
|> Jason.decode!()
end
end
# # calling it like:
# credentials = Salesforce.get_credentials()
# accounts = Salesforce.query(credentials, "SELECT Id, Name FROM Account")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment