# Subaccounts Calls

## Introduction

Mandrill Subaccounts can be mapped on Mailjet sub API keys manageable with [`apikey`](/docs/api-reference/email-api/categories/settings/). The Mailjet REST API lets you manage multiple API key. Each API key will have its own contacts, lists, newsletters and statistics dedicated database. The contacts and lists will not be shared between API keys (including the main API key)..

## List

Info
Maps to /subaccounts/list.json

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

Bash
```bash
# View : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
curl -s \
	-X GET \
	--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
	https://api.mailjet.com/v3/REST/apikey
```

Node.js
```javascript
/**
 *
 * View : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
 *
 */
var mailjet = require('node-mailjet').connect(
  process.env.MJ_APIKEY_PUBLIC,
  process.env.MJ_APIKEY_PRIVATE
)
var request = mailjet.get('apikey').request()
request
  .on('success', function(response, body) {
    console.log(response.statusCode, body)
  })
  .on('error', function(err, response) {
    console.log(response.statusCode, err)
  })
```

Ruby
```ruby
# View : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
Mailjet.configure do |config|
  config.api_key = ENV['MJ_APIKEY_PUBLIC']
  config.secret_key = ENV['MJ_APIKEY_PRIVATE']
  config.default_from = 'your default sending address'
end
variable = Mailjet::Apikey.all()
```

Python
```python
"""
View : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
"""
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))
result = mailjet.apikey.get()
print result.status_code
print result.json()
```

Go
```go
/*
View : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
*/
package main
import (
	"fmt"
	. "github.com/mailjet/mailjet-apiv3-go"
	"github.com/mailjet/mailjet-apiv3-go/resources"
	"os"
)
func main () {
	mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
	var data []resources.Apikey
	_, _, err := mailjetClient.List("apikey", &data)
	if err != nil {
	  fmt.Println(err)
	}
	fmt.Printf("Data array: %+v\n", data)
}
```

Java
```java
package com.my.project;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.MailjetClient;
import com.mailjet.client.MailjetRequest;
import com.mailjet.client.MailjetResponse;
import com.mailjet.client.resource.Apikey;
public class MyClass {
    /**
     * View : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
     */
    public static void main(String[] args) throws MailjetException {
      MailjetClient client;
      MailjetRequest request;
      MailjetResponse response;
      client = new MailjetClient("api key", "api secret");
      request = new MailjetRequest(Apikey.resource);
      response = client.get(request);
      System.out.println(response.getStatus());
      System.out.println(response.getData());
    }
}
```

```json
{
  "Count": 1,
  "Data": [
    {
      "ACL": "",
      "APIKey": "",
      "CreatedAt": "",
      "ID": "",
      "IsActive": "false",
      "IsMaster": "false",
      "Name": "",
      "QuarantineValue": "",
      "Runlevel": "Normal",
      "SecretKey": "",
      "TrackHost": "",
      "UserID": ""
    }
  ],
  "Total": 1
}
```

Issue a `GET` request on the [`apikey`](/docs/api-reference/email-api/categories/settings/) resource

## Add

Info
Maps to /subaccounts/add.json

PHP
```php
<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$body = [
    'Name' => "MynewKEY"
];
$response = $mj->post(Resources::$Apikey, ['body' => $body]);
$response->success() && var_dump($response->getData());
?>
```

Bash
```bash
# Create : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
curl -s \
	-X POST \
	--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
	https://api.mailjet.com/v3/REST/apikey \
	-H 'Content-Type: application/json' \
	-d '{
		"Name":"MynewKEY"
	}'
```

Node.js
```javascript
/**
 *
 * Create : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
 *
 */
var mailjet = require('node-mailjet').connect(
  process.env.MJ_APIKEY_PUBLIC,
  process.env.MJ_APIKEY_PRIVATE
)
var request = mailjet.post('apikey').request({
  Name: 'MynewKEY',
})
request
  .on('success', function(response, body) {
    console.log(response.statusCode, body)
  })
  .on('error', function(err, response) {
    console.log(response.statusCode, err)
  })
```

Ruby
```ruby
# Create : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
Mailjet.configure do |config|
  config.api_key = ENV['MJ_APIKEY_PUBLIC']
  config.secret_key = ENV['MJ_APIKEY_PRIVATE']
  config.default_from = 'your default sending address'
end
variable = Mailjet::Apikey.create(name: "MynewKEY")
```

Python
```python
"""
Create : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
"""
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))
data = {
  'Name': 'MynewKEY'
}
result = mailjet.apikey.create(data=data)
print result.status_code
print result.json()
```

Go
```go
/*
Create : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
*/
package main
import (
	"fmt"
	. "github.com/mailjet/mailjet-apiv3-go"
	"github.com/mailjet/mailjet-apiv3-go/resources"
	"os"
)
func main () {
	mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
	var data []resources.Apikey
	mr := &MailjetRequest{
	  Resource: "apikey",
	}
	fmr := &FullMailjetRequest{
	  Info: mr,
	  Payload: &resources.Apikey {
      Name: "MynewKEY",
    },
	}
	err := mailjetClient.Post(fmr, &data)
	if err != nil {
	  fmt.Println(err)
	}
	fmt.Printf("Data array: %+v\n", data)
}
```

Java
```java
package com.my.project;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.MailjetClient;
import com.mailjet.client.MailjetRequest;
import com.mailjet.client.MailjetResponse;
import com.mailjet.client.resource.Apikey;
public class MyClass {
    /**
     * Create : Manage your Mailjet API Keys. API keys are used as credentials to access the API and SMTP server.
     */
    public static void main(String[] args) throws MailjetException {
      MailjetClient client;
      MailjetRequest request;
      MailjetResponse response;
      client = new MailjetClient("api key", "api secret");
      request = new MailjetRequest(Apikey.resource)
						.property(Apikey.NAME, "MynewKEY");
      response = client.post(request);
      System.out.println(response.getStatus());
      System.out.println(response.getData());
    }
}
```

```json
{
  "Count": 1,
  "Data": [
    {
      "ACL": "",
      "APIKey": "",
      "CreatedAt": "",
      "ID": "",
      "IsActive": "false",
      "IsMaster": "false",
      "Name": "",
      "QuarantineValue": "",
      "Runlevel": "Normal",
      "SecretKey": "",
      "TrackHost": "",
      "UserID": ""
    }
  ],
  "Total": 1
}
```

To create a new API key , you just need to do a POST request on the [`apikey`](/docs/api-reference/email-api/categories/settings/).

The response will contain a new APIKey and SecretKey. Please allow a few seconds before running any additional API request on this new APIkey as the full setup process of this new sub-account will take longer than the API call.