Salesforce Integration
MarketingCRM image automation with ButterflyAPI
Apex Integration
Apex Callout Class
// Apex Class for ButterflyAPI
public class ButterflyAPIService {
private static final String API_ENDPOINT = 'https://butterflyapi.com/api/v1/run';
private static final String API_KEY = 'YOUR_API_KEY'; // Use Named Credential
@future(callout=true)
public static void enhanceImage(Id attachmentId) {
Attachment att = [SELECT Id, Body, Name FROM Attachment WHERE Id = :attachmentId];
// Upload to temp storage and get URL
String imageUrl = uploadToTempStorage(att.Body);
HttpRequest req = new HttpRequest();
req.setEndpoint(API_ENDPOINT);
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + API_KEY);
req.setHeader('Content-Type', 'application/json');
req.setBody(JSON.serialize(new Map<String, Object>{
'api' => 'image-enhance',
'input' => new Map<String, Object>{
'image_url' => imageUrl,
'scale' => 2
}
}));
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
Map<String, Object> output = (Map<String, Object>)result.get('output');
String enhancedUrl = (String)output.get('image_url');
// Download and update attachment
updateAttachment(attachmentId, enhancedUrl);
}
}
}