Lightweight wrapper around OVH's APIs. Handles all the hard work including credential creation and requests signing.
using Ovh.Api;
using System;
namespace api_tester
{
class Program
{
static async Task Main(string[] args)
{
// Instantiate. Visit https://api.ovh.com/createToken/index.cgi?GET=/me
// to get your credentials
Client client = new Client("ovh-eu", "<application_key>", "<application_secret>", "<consumer_key>");
PartialMe me = await client.GetAsync<PartialMe>("/me");
// Print nice welcome message
Console.WriteLine(String.Format("Hello {0}!", me.firstname));
Console.ReadLine();
}
}
class PartialMe
{
public string firstname { get; set; }
public string name { get; set; }
}
}
The easiest way to get the latest stable release is to grab it from NuGet.
nuget install csharp-ovh
To interact with the APIs, the SDK needs to identify itself using an
application_key
and an application_secret
. To get them, you need
to register your application. Depending the API you plan to use, visit:
- OVH Europe
- OVH US
- OVH North-America
- So you Start Europe
- So you Start North America
- Kimsufi Europe
- Kimsufi North America
- RunAbove
Once created, you will obtain an application key (AK) and an application secret (AS).
The easiest and safest way to use your application's credentials is to create a
.ovh.conf
configuration file in application's working directory. Here is how
it looks like:
[default]
; general configuration: default endpoint
endpoint=ovh-eu
[ovh-eu]
; configuration specific to 'ovh-eu' endpoint
application_key=my_app_key
application_secret=my_application_secret
; uncomment following line when writing a script application
; with a single consumer key.
;consumer_key=my_consumer_key
Depending on the API you want to use, you may set the endpoint
to:
ovh-eu
for OVH Europe APIovh-us
for OVH US APIovh-ca
for OVH North-America APIsoyoustart-eu
for So you Start Europe APIsoyoustart-ca
for So you Start North America APIkimsufi-eu
for Kimsufi Europe APIkimsufi-ca
for Kimsufi North America APIrunabove-ca
for RunAbove API
See Configuration for more information on available configuration mechanisms.
Note
When using a versioning system, make sure to add .ovh.conf
to ignored
files. It contains confidential/security-sensitive informations!
To allow your application to access a customer account using the API on your behalf, you need a consumer key (CK).
Here is a sample code you can use to allow your application to access a customer's informations:
using Ovh.Api;
using Ovh.Api.Models;
using System;
using System.Collections.Generic;
namespace api_tester
{
class Program
{
static async Task Main(string[] args)
{
Client client = new Client();
CredentialRequest requestPayload = new CredentialRequest(
new List<AccessRight>(){
new AccessRight("GET", "/me")
},
"https://redirect.url" // Change this URL if you don't want to see an unreachable webpage after you validated your consumer key. An unreachable webpage does not mean that the validation has failed.
);
CredentialRequestResult credentialRequestResult =
await client.RequestConsumerKeyAsync(requestPayload);
Console.Write(
String.Format("Please visit {0} to authenticate ",
credentialRequestResult.ValidationUrl));
Console.WriteLine("and press enter to continue");
Console.ReadLine();
client.ConsumerKey = credentialRequestResult.ConsumerKey;
PartialMe me = await client.GetAsync<PartialMe>("/me");
Console.WriteLine(
String.Format("Welcome, {0}", me.firstname));
Console.WriteLine(
String.Format("Btw, your 'consumerKey' is {0}",
credentialRequestResult.ConsumerKey));
Console.ReadLine();
}
}
class PartialMe
{
public string firstname { get; set; }
public string name { get; set; }
}
}
Returned consumerKey
should then be kept to avoid re-authenticating your
end-user on each use.
Note
To request full and unlimited access to the API, you may use wildcards:
new List<AccessRight>(){
new AccessRight("GET", "/*"),
new AccessRight("PUT", "/*"),
new AccessRight("POST", "/*"),
new AccessRight("DELETE", "/*"),
}
e-mail redirections may be freely configured on domains and DNS zones hosted by
OVH to an arbitrary destination e-mail using API call
POST /email/domain/{domain}/redirection
.
using Ovh.Api;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace api_tester
{
class Program
{
static void Main(string[] args)
{
Client client = new Client();
string domain = "<someDomain>";
string source = "<sourceEmail>";
string destination = "<destinationEmail>";
Dictionary<string, object> payload = new Dictionary<string, object>();
payload.Add("from", source);
payload.Add("to", destination);
payload.Add("localCopy", false);
client.PostAsync(
String.Format("/email/domain/{0}/redirection", domain),
payload
).Wait();
Console.WriteLine(
String.Format("Installed new mail redirection from {0} to {1}",
source, destination));
Console.ReadLine();
}
}
}
Let's say you want to integrate OVH bills into your own billing system, you
could just script around the /me/bills
endpoints and even get the details
of each bill lines using /me/bill/{billId}/details/{billDetailId}
.
This example assumes an existing Configuration with valid application_key
,
application_secret
and consumer_key
.
using Ovh.Api;
using System;
using System.Collections.Generic;
namespace api_tester
{
class Program
{
static async Task Main(string[] args)
{
Client client = new Client();
var billIds = await client.GetAsync<List<string>>("/me/bill");
foreach (var billId in billIds)
{
PartialOvhBill details = await client.GetAsync<PartialOvhBill>("/me/bill/" billId);
Console.WriteLine(
String.Format("{0} ({1}): {2} --> {3}",
billId, details.date, details.priceWithTax.text, details.pdfUrl));
}
Console.ReadLine();
}
}
class PartialOvhBill
{
public string date { get; set; }
public string pdfUrl { get; set; }
public OvhPrice priceWithTax { get; set; }
}
class OvhPrice
{
public string currencyCode { get; set; }
public double value { get; set; }
public string text { get; set; }
}
}
'Network burst' is a free service but is opt-in. What if you have, say, 10
servers in SBG-1
datacenter? You certainely don't want to activate it
manually for each servers. You could take advantage of a code like this.
This example assumes an existing Configuration with valid application_key
,
application_secret
and consumer_key
.
using Ovh.Api;
using System;
using System.Collections.Generic;
namespace api_tester
{
class Program
{
static async Task Main(string[] args)
{
Client client = new Client();
var serverIds = await client.GetAsync<List<string>>("/dedicated/server/");
foreach (var serverId in serverIds)
{
string serverUrl = "/dedicated/server/" serverId;
var details = await client.GetAsync<PartialDedicatedServer>(serverUrl);
if (details.datacenter == "sbg1")
{
await client.PutStringAsync(serverUrl "/burst", "{\"status\":\"active\"}");
Console.WriteLine("Burst enabled on server " serverId);
}
}
Console.ReadLine();
}
}
class PartialDedicatedServer
{
public string datacenter { get; set; }
}
}
Thanks to the application key / consumer key mechanism, it is possible to finely track applications having access to your data and revoke this access. This examples lists validated applications. It could easily be adapted to manage revocation too.
This example assumes an existing Configuration with valid application_key
,
application_secret
and consumer_key
.
using Ovh.Api;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
namespace api_tester
{
class Program
{
static async Task Main(string[] args)
{
Client client = new Client();
QueryStringParams qsp = new QueryStringParams();
qsp.Add("status", "validated");
var credentialIds = await client.GetAsync<List<string>>("/me/api/credential", qsp);
foreach (var credentialId in credentialIds)
{
string credentialUrl = "/me/api/credential/" credentialId;
var credential = await client.GetAsync<PartialCredential>(credentialUrl);
var application = await client.GetAsync<PartialApplication>(credentialUrl "/application");
StringBuilder sb = new StringBuilder();
sb.Append(credentialId).Append(" ").Append(application.status)
.Append(" ").Append(application.name).Append(" ")
.Append(application.description).Append(" ")
.Append(credential.creation).Append(" ")
.Append(credential.expiration).Append(" ")
.Append(credential.lastUse);
Console.WriteLine(sb.ToString());
}
Console.ReadLine();
}
}
class PartialCredential
{
public string creation{ get; set; }
public string expiration { get; set; }
public string lastUse { get; set; }
}
class PartialApplication
{
public string name { get; set; }
public string description { get; set; }
public string status { get; set; }
}
}
The straightforward way to use OVH's API keys is to embed them directly in the application code. While this is very convenient, it lacks of elegance and flexibility.
Alternatively it is suggested to use configuration files or environment variables so that the same code may run seamlessly in multiple environments. Production and development for instance.
This wrapper will first look for direct instanciation parameters then
OVH_ENDPOINT
, OVH_APPLICATION_KEY
, OVH_APPLICATION_SECRET
and
OVH_CONSUMER_KEY
environment variables. If either of these parameter is not
provided, it will look for a configuration file of the form:
[default]
; general configuration: default endpoint
endpoint=ovh-eu
[ovh-eu]
; configuration specific to 'ovh-eu' endpoint
application_key=my_app_key
application_secret=my_application_secret
consumer_key=my_consumer_key
The client will successively attempt to locate this configuration file in
- Current working directory:
./.ovh.conf
- Current user's home directory
%USERPROFILE%/.ovh.conf
This lookup mechanism makes it easy to overload credentials for a specific project or user.
- Documentation: https://eu.api.ovh.com/
- Community support: [email protected]
- Console: https://eu.api.ovh.com/console
- Create application credentials: https://eu.api.ovh.com/createApp/
- Create script credentials (all keys at once): https://eu.api.ovh.com/createToken/
- Documentation: https://api.us.ovhcloud.com/
- Community support: [email protected]
- Console: https://api.us.ovhcloud.com/console/
- Create application credentials: https://api.us.ovhcloud.com/createApp/
- Create script credentials (all keys at once): https://api.us.ovhcloud.com/createToken/
- Documentation: https://ca.api.ovh.com/
- Community support: [email protected]
- Console: https://ca.api.ovh.com/console
- Create application credentials: https://ca.api.ovh.com/createApp/
- Create script credentials (all keys at once): https://ca.api.ovh.com/createToken/
- Documentation: https://eu.api.soyoustart.com/
- Community support: [email protected]
- Console: https://eu.api.soyoustart.com/console/
- Create application credentials: https://eu.api.soyoustart.com/createApp/
- Create script credentials (all keys at once): https://eu.api.soyoustart.com/createToken/
- Documentation: https://ca.api.soyoustart.com/
- Community support: [email protected]
- Console: https://ca.api.soyoustart.com/console/
- Create application credentials: https://ca.api.soyoustart.com/createApp/
- Create script credentials (all keys at once): https://ca.api.soyoustart.com/createToken/
- Documentation: https://eu.api.kimsufi.com/
- Community support: [email protected]
- Console: https://eu.api.kimsufi.com/console/
- Create application credentials: https://eu.api.kimsufi.com/createApp/
- Create script credentials (all keys at once): https://eu.api.kimsufi.com/createToken/
- Documentation: https://ca.api.kimsufi.com/
- Community support: [email protected]
- Console: https://ca.api.kimsufi.com/console/
- Create application credentials: https://ca.api.kimsufi.com/createApp/
- Create script credentials (all keys at once): https://ca.api.kimsufi.com/createToken/