Hey there, fellow developer! Ready to dive into the world of Salesforce Marketing Cloud API integration? You're in for a treat. This powerful API opens up a whole new realm of possibilities for your marketing automation. Let's get cracking and build something awesome in Ruby!
Before we jump in, make sure you've got:
Let's kick things off:
# Create a new Ruby project mkdir sfmc_api_integration cd sfmc_api_integration # Create a Gemfile echo "source 'https://rubygems.org' gem 'httparty' gem 'dotenv'" > Gemfile # Install the gems bundle install
First things first, let's get that access token:
require 'httparty' require 'dotenv/load' def get_access_token response = HTTParty.post( "https://#{ENV['SFMC_SUBDOMAIN']}.auth.marketingcloudapis.com/v2/token", body: { grant_type: 'client_credentials', client_id: ENV['SFMC_CLIENT_ID'], client_secret: ENV['SFMC_CLIENT_SECRET'] } ) response.parsed_response['access_token'] end
Pro tip: Implement a token refresh mechanism to keep your integration running smoothly!
Now, let's make some API calls:
def get_request(endpoint) HTTParty.get( "https://#{ENV['SFMC_SUBDOMAIN']}.rest.marketingcloudapis.com/#{endpoint}", headers: { 'Authorization' => "Bearer #{get_access_token}", 'Content-Type' => 'application/json' } ) end def post_request(endpoint, payload) HTTParty.post( "https://#{ENV['SFMC_SUBDOMAIN']}.rest.marketingcloudapis.com/#{endpoint}", headers: { 'Authorization' => "Bearer #{get_access_token}", 'Content-Type' => 'application/json' }, body: payload.to_json ) end
Here are a few examples to get your creative juices flowing:
# Retrieve subscriber data def get_subscriber(email) get_request("data/v1/customobjectdata/key/#{ENV['SUBSCRIBERS_DE_KEY']}/rowset?$filter=EmailAddress%20eq%20'#{email}'") end # Create/update data extension def upsert_data_extension(de_key, data) post_request("data/v1/async/dataextensions/key:#{de_key}/rows", data) end # Trigger email send def trigger_email_send(email, template_id) payload = { "To": { "Address": email, "SubscriberKey": email }, "Options": { "RequestType": "SYNC" } } post_request("messaging/v1/messageDefinitionSends/key:#{template_id}/send", payload) end
Don't forget to add some error handling and logging. Your future self will thank you!
def api_request(method, endpoint, payload = nil) response = method == :get ? get_request(endpoint) : post_request(endpoint, payload) if response.success? puts "Success: #{response.code}" response.parsed_response else puts "Error: #{response.code} - #{response.body}" nil end rescue StandardError => e puts "Exception: #{e.message}" nil end
Always test your code. Here's a quick example using RSpec:
RSpec.describe 'SFMC API Integration' do it 'successfully retrieves an access token' do token = get_access_token expect(token).to be_a(String) expect(token.length).to be > 0 end end
And there you have it! You're now armed with the knowledge to build a robust Salesforce Marketing Cloud API integration in Ruby. Remember, this is just the beginning. There's so much more you can do with this API, so keep exploring and building amazing things!
Happy coding, and may your API calls always return 200 OK! 🚀