# SPF and DKIM validation

SPF & DKIM are authentication systems that tell Internet Service Providers (ISPs), like Gmail and Yahoo, that incoming mail has been sent from an authorized system, and that it is not spam or email spoofing. To set Mailjet as an authorized sender and improve your deliverability, you need to modify your DNS records to include DKIM signature and SPF.

To retrieve your SPF and DKIM authentication records, do a `GET` on `/dns/{domain_id or domain_name}`.

### Example

cURL
```shell
curl -s \
	-X GET \
	--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
	https://api.mailjet.com/v3/REST/dns/$dns_ID \
```

PHP
```php
<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'),true,['version' => 'v3']);
$response = $mj->get(Resources::$Dns, ['id' => $id]);
$response->success() && var_dump($response->getData());
?>
```

Node.js
```javascript
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.get("dns", {'version': 'v3'})
	.id($dns_ID)
	.request()
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})
```

Ruby
```ruby
require 'mailjet'
Mailjet.configure do |config|
  config.api_key = ENV['MJ_APIKEY_PUBLIC']
  config.secret_key = ENV['MJ_APIKEY_PRIVATE']
  config.api_version = "v3"
end
variable = Mailjet::Dns.find($dns_ID
)
p variable.attributes['Data']
```

Python
```python
from mailjet_rest import Client
import os
api_key = os.environ['MJ_APIKEY_PUBLIC']
api_secret = os.environ['MJ_APIKEY_PRIVATE']
mailjet = Client(auth=(api_key, api_secret), version='v3')
id = '$dns_ID'
result = mailjet.dns.get(id=id)
print result.status_code
print result.json()
```

Java
```java
package com.my.project;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.errors.MailjetSocketTimeoutException;
import com.mailjet.client.MailjetClient;
import com.mailjet.client.MailjetRequest;
import com.mailjet.client.MailjetResponse;
import com.mailjet.client.ClientOptions;
import com.mailjet.client.resource.Dns;
import org.json.JSONArray;
import org.json.JSONObject;
public class MyClass {
    public static void main(String[] args) throws MailjetException, MailjetSocketTimeoutException {
      MailjetClient client;
      MailjetRequest request;
      MailjetResponse response;
      client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions("v3"));
      request = new MailjetRequest(Dns.resource, ID);
      response = client.get(request);
      System.out.println(response.getStatus());
      System.out.println(response.getData());
    }
}
```

Go
```go
package main
import (
	"fmt"
	"log"
	"os"
	mailjet "github.com/mailjet/mailjet-apiv3-go"
	"github.com/mailjet/mailjet-apiv3-go/resources"
)
func main () {
	mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
	var data []resources.Dns
	mr := &Request{
	  Resource: "dns",
	  ID: RESOURCE_ID,
	}
	err := mailjetClient.Get(mr, &data)
	if err != nil {
	  fmt.Println(err)
	}
	fmt.Printf("Data array: %+v\n", data)
}
```

C#
```csharp
using Mailjet.Client;
using Mailjet.Client.Resources;
using System;
using Newtonsoft.Json.Linq;
namespace Mailjet.ConsoleApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         RunAsync().Wait();
      }
      static async Task RunAsync()
      {
         MailjetClient client = new MailjetClient(Environment.GetEnvironmentVariable("MJ_APIKEY_PUBLIC"), Environment.GetEnvironmentVariable("MJ_APIKEY_PRIVATE"))
         {
            Version = ApiVersion.V3,
         };
         MailjetRequest request = new MailjetRequest
         {
            Resource = Dns.Resource,
            ResourceId = ResourceId.Numeric(ID)
         }
         MailjetResponse response = await client.GetAsync(request);
         if (response.IsSuccessStatusCode)
         {
            Console.WriteLine(string.Format("Total: {0}, Count: {1}\n", response.GetTotal(), response.GetCount()));
            Console.WriteLine(response.GetData());
         }
         else
         {
            Console.WriteLine(string.Format("StatusCode: {0}\n", response.StatusCode));
            Console.WriteLine(string.Format("ErrorInfo: {0}\n", response.GetErrorInfo()));
            Console.WriteLine(response.GetData());
            Console.WriteLine(string.Format("ErrorMessage: {0}\n", response.GetErrorMessage()));
         }
      }
   }
}
```

### API Response

```
{
	"Count": 1,
	"Data": [
		{
			"DKIMRecordName": "mailjet._domainkey.example.com.",
			"DKIMRecordValue": "k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB",
			"DKIMStatus": "OK",
			"Domain": "example.com",
			"ID": 1234,
			"IsCheckInProgress": false,
			"LastCheckAt": "2018-01-01T00:00:00",
			"OwnerShipToken": "123a4bc56d7890efg123h4ijk56l78mn",
			"OwnerShipTokenRecordName": "mailjet._123a4bc56.example.com.",
			"SPFRecordValue": "v=spf1 include:spf.mailjet.com ?all",
			"SPFStatus": "OK"
		}
	],
	"Total": 1
}
```

The information you need from the response is:

- `DKIMRecordName`: contains the name of the DNS txt record for the DKIM configuration
- `DKIMRecordValue`: contains the value of the DNS txt record for the domain DKIM configuration
- `DKIMStatus`: last result of the domain DKIM configuration check ran (via the check action or a periodic check on Mailjet side). The status can be Not checked, OK or Error.
- `SPFRecordValue`: contains the name of the DNS txt record for the SPF configuration. Please note that this field contains the default value, not taking into account existing SPF records for the domain. Concatenating this with the existing value is left up to the API client.
- `SPFStatus`: last result of the domain SPF configuration check ran (via the check action or a periodic check on Mailjet side). The status can be Not checked, OK or Error.


Follow the "How to setup DomainKeys (DKIM) and SPF in my DNS records" guide to learn how to use this information to modify your DNS records.

Once you have finished the setup of your DNS records, do a `POST` request on `/dns/{domain_ID or domain_name}/check` to complete the validation.