+ Start a Discussion
Keith Stephens 18Keith Stephens 18 

SendGrid with Salesforce

Hello All,
I think I need to code your own HTTP Post Request, formatted as JSON.
In order to call SendGrids email API's from my Salesforce Class, but I do not know how to do this.
Can anyone help me with the code that will do this?  I think it would be something similar to this:
string strUsername = 'testusername';
        string strPwd = 'testpwd';

        Http m_http = new Http();
        HttpRequest req = new HttpRequest();
        
            
        req.setEndpoint('"https://api.sendgrid.com/v3/mail/send');
        req.setHeader('Content-Type: application/json'); 
        req.setHeader('Authorization: Bearer SendGridAPIKEY');          
        req.setMethod('POST');
        req.setBody(content);
        
        httpResponse response = m_http.send(req);
Thanks,
Keith.
 
Best Answer chosen by Keith Stephens 18
Keith Stephens 18Keith Stephens 18
I solved my issue.
Thanks everyone.
 

All Answers

Raj VakatiRaj Vakati
You are doing on the write way .. but you have an wrapper to make an API calls from the salesforce to send grid
 Refer this link

https://github.com/sendgrid/sendgrid-apex
String body = this.credentialsToWebFormat() + email.toWebFormat();

    HttpRequest req = new HttpRequest();
    Http http = new Http();

    req.setEndpoint(API_URL);
    req.setMethod('POST');
    req.setHeader('Content-Type','application/x-www-form-urlencoded');
    req.setBody(body);
    req.setHeader('Content-Length',String.valueof(body.length()));



 
Keith Stephens 18Keith Stephens 18
Hello Raj,
I have that github project installed in my SF sandbox but it gives me errors and is not working, and no reply from the author or from another post I have in this forum.  So that is why I am trying to use SendGrids API, and trying to create my own HTTP Post.
So with you example how does this fit in with SendGrid, how to I get my TO:, and FROM email address and the email text which is the body.
Thanks
Keith.
Keith Stephens 18Keith Stephens 18
I solved my issue.
Thanks everyone.
 
This was selected as the best answer